名为类别的表 1 包含 70 条记录 名为 FilterCategorys 的表 2 包含 0 条记录(当前)。
我的 lambda 连接,我只想提取不匹配的记录,所以在这种情况下,我希望得到 70 条记录。这是我不正确的 Lambda:
var filteredList = categorys
.Join(filterCategorys,
x => x.Id,
y => y.CategoryId,
(x, y) => new { catgeory = x, filter = y })
.Where(xy => xy.catgeory.Id != xy.filter.CategoryId)
.Select(xy => new Category()
{
Name = xy.catgeory.Name,
Id = xy.catgeory.Id,
ParentCategoryId = xy.catgeory.ParentCategoryId
})
.ToList();
我需要的正确语法是什么?
最佳答案
不确定您是否需要使用 lambda(而不是查询语法),但我更喜欢具有外连接的语句的查询语法。
这应该是等价的:
var filteredList = (
from c in Categorys
join fc in FilterCategorys on c.Id equals fc.CategoryId into outer
from o in outer.DefaultIfEmpty()
select new
{
Category = new Category
{
Name = c.Name,
Id = c.Id,
ParentCategoryId = c.ParentCategoryId
},
Exists = (o != null)
})
.Where(c => !c.Exists)
.Select(c => c.Category);
关于c# - Lambda 在加入时不相等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30621416/