我一直坚持选择不同数据的查询。
假设数据
但他们有不同的ID (Primary key ID on SQL Table)
这是我到目前为止所尝试的内容
var query = (from a in db.Employees
join b in db.People on a.PersonId equals b.PersonId
where a.IsSeparated != true
select b).Distinct();
和
var query = db.Employees.Join(db.People, x => x.PersonId, y => y.PersonId, (x, y) => new { x, y })
.Where(z => z.x.IsSeparated != true).GroupBy(t => t.y.CompleteName).Select(x => x.FirstOrDefault());
但两个查询仍显示所有数据。在我的查询中我想要的是,如果存在重复数据使其不同。
如果你们评论lambda,请解释我还在学习如何使用它。
答案 0 :(得分:2)
您可以尝试使用以下LINQ:
var query = (from a in db.Employees
join b in db.People on a.PersonId equals b.PersonId
where a.IsSeparated != true
select b).GroupBy(x=> new { x.PersonId}).Select(x=>x.First());
答案 1 :(得分:0)
var query = (from a in db.Employees
join b in db.People on a.PersonId equals b.PersonId
where a.IsSeparated != true
select b)
.GroupBy(x=> x.CompleteName)
.Select(grp => grp.Value.First());