繁体   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