簡體   English   中英

從每個語句中加入兩個列表

[英]Joining two lists from each statement

在此處輸入圖像描述

if (Settings.Default.All)
{
      List = new ObservableCollection<LexisNexis>(UnitOfWork.Query.Lexis.LexisForApprove2().OrderBy(x => x.TxnID).Reverse());             
}
if (Settings.Default.MLhuillier)
{
     List = new ObservableCollection<LexisNexis>(UnitOfWork.Query.Lexis.LexisForApprove2().Where(x => x.ServiceMode == "MLhuillier").OrderBy(x => x.TxnID).Reverse());
}
if (Settings.Default.BPI)
{
    List = new ObservableCollection<LexisNexis>(UnitOfWork.Query.Lexis.LexisForApprove2().Where(x => x.ServiceMode == "BPI").OrderBy(x => x.TxnID).Reverse());
}

我想組合每個返回 true 的 if 語句中的每個列表。 我的程序只返回最后一個列表。 TYIA

簡化代碼

以下應該做你想要的,幾乎沒有重復,最多一次遍歷LexisForApprove2

var orFilters = Settings.Default.All ? null : new List<string>();

if (!Settings.Default.All)
{
    if (Settings.Default.MLhuillier) orFilters.Add("MLhuillier");
    if (Settings.Default.BPI) orFilters.Add("BPI");
}

var l = orFilters == null
    ? UnitOfWork.Query.Lexis.LexisForApprove2() // Everything
    : orFilters.Any() 
        ? UnitOfWork.Query.Lexis.LexisForApprove2().Where(x => orFilters.Contains(x.ServiceMode))
        : new List<LexisNexis>(); // Not 'All' but no others allowed

List = new ObservableCollection<LexisNexis>(l.OrderByDescending(y => y.TxnID));

Distinct

僅作記錄,在這種情況下不推薦,您可以使用 List 的AddRange或 Linq 的Union后跟Distinct ,如果LexisNexis對象擅長將自己與其他對象進行比較,這將起作用:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM