过滤的LINQ除了在性能 [英] Filter Linq EXCEPT on properties

查看:122
本文介绍了过滤的LINQ除了在性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能看起来很傻,但所有的例子,我发现在使用LINQ使用EXCEPT两个列表或仅字符串或整数数组和过滤他们根据比赛中,例如:

This may seem silly, but all the examples I've found for using EXCEPT in linq use two lists or arrays of only strings or integers and filters them based on the matches, for example:

VAR排除= users.Except(匹配);

我想用排除让我的code简短,但似乎无法找出如何做到以下几点:

I want to use exclude to keep my code short and simple, but can't seem to find out how to do the following:

class AppMeta
{
  public int Id {get;set;}
}

var excludedAppIds = new List<int> {2, 3, 5, 6};
var unfilteredApps = new List<AppMeta>
                         {
                           new AppMeta {Id = 1}
                           new AppMeta {Id = 2}
                           new AppMeta {Id = 3}
                           new AppMeta {Id = 4}
                           new AppMeta {Id = 5}
                         }

我如何获得AppMeta的一个列表上excludedAppIds过滤器?

How do I get a list of AppMeta back that filters on excludedAppIds?

推荐答案

尝试一个简单的查询在哪里

Try a simple where query

var filtered = unfilteredApps.Where(i => !excludedAppIds.Contains(i.Id)); 

除了该方法采用平等,你的列表包含不同类型的对象,所以没有它们包含将相等的物品!

The except method uses equality, your lists contain objects of different types, so none of the items they contain will be equal!

这篇关于过滤的LINQ除了在性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆