簡體   English   中英

LINQ:從IQueryable中刪除項目

[英]LINQ: Remove items from IQueryable

我想在使用數據綁定之前從LINQ查詢的結果中刪除一個項目。 這樣做的正確方法是什么?

我插圖中的foreach是我的問題的主題。 插圖:

var obj =
    (from a in dc.Activities
    where a.Referrer != null
    && a.Referrer.Trim().Length > 12
    && a.Session.IP.NumProblems == 0
    && (a.Session.UID == null || a.Session.UID < 1 || a.Session.User.BanLevel < 1)
    select a)
    .Take(int.Parse(ConfigurationManager.AppSettings["RecentItemQty"]));
foreach (Activity act in obj)
    if (isDomainBlacklisted(ref dc, act.Referrer))
        obj.Remove(act);

你不需要foreach就可以使用這個......

obj.RemoveAll(act => isDomainBlackListed(ref dc, act.Referrer));

您可以將它放在查詢的末尾,以便在它們最終結果之前將其過濾掉:

var obj =
   (from a in dc.Activities
   where a.Referrer != null
   && a.Referrer.Trim().Length > 12
   && a.Session.IP.NumProblems == 0
   && (a.Session.UID == null || a.Session.UID < 1 || a.Session.User.BanLevel < 1)
   select a)
   .Take(int.Parse(ConfigurationManager.AppSettings["RecentItemQty"]))
   .Where(a => !isDomainBlacklisted(ref dc, a.Referrer));

你可以把Where的前Take ,如果你想其他項目替換過濾掉的,但是這意味着,當然isDomainBlacklisted更多的電話。

暫無
暫無

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

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