我有一个包含Name和TypeId的包含列表,我想为它创建一个包含某些TypeId的给定项目的排除列表。但是,如果名称根本没有出现在列表中,那么它就不会被排除在任何类型之外。
如何使用linq执行此操作?
List<NameTypeFilter> includeList = new List<NameTypeFilter>();
includeList.Add(new NameTypeFilter
{
Name = "N1",
Type = "T1"
});
MyItem myItem1 = new MyItem();
myItem1.Type = "T1";
MyItem myItem2 = new MyItem();
myItem1.Type = "T2";
MyItem myItem3 = new MyItem();
myItem1.Type = "T3";
// excludelist for myItem1 should contain nothing
// excludelist for myItem2 should contain N1
// excludelist for myItem3 should contain N1
includeList.Add(new NameTypeFilter
{
Name = "N2",
Type = "T2"
});
// excludelist for myItem1 should contain N2
// excludelist for myItem2 should contain N1
// excludelist for myItem3 should contain N1,N2
答案 0 :(得分:0)
如果您共享代码示例,将有助于更详细地了解您的查询 但是根据我的假设,如果你使用TakeWhile或Linq的SkipWhile添加Linq条件,它可能会帮助你获得欲望结果
答案 1 :(得分:0)
includeList.Where(y => !includeList.Any(z => z.Type == currentItemType && y.Name == z.Name))
我认为这样做