我有一个返回数据的LINQ查询,我想根据通常相同的2个字段(fkProfileID和fkOrgID)进行过滤。我只想在fkProfileID和fkOrgID匹配时显示1条记录,因此它类似于SQL不同,但是在2个字段而不是1个字段。
我的数据将如下所示(其他字段已被删除):
fkProfileID fkOrgID
1 1001
1 1001
1 1001
2 1001
2 1001
1 1005
1 1005
所以在这里我想只返回以下内容:
fkProfileID fkOrgID
1 1001
2 1001
1 1005
这是我目前的LINQ(以上两个字段都在tblUserRights中),我如何更改它才能执行此操作?
List<ProfileJSON> lstProfiles = (from r in _database.tblUserRights
join p in _database.LuProfiles on r.fkProfileID equals p.luProfileID
join o in _database.tblOrganisations on r.fkOrgID equals o.pkOrgID
where r.fkUniqueID == intPKUserID
orderby o.OrgDesc, p.ProfileName
select new ProfileJSON
{
SiteID = o.pkOrgID,
SiteName = o.OrgDesc,
ProfileID = p.luProfileID,
ProfileName = p.ProfileName
}).ToList();
答案 0 :(得分:0)
您可以在查询后使用.Distinct()
List<ProfileJSON> lstProfiles = (from r in _database.tblUserRights
join p in _database.LuProfiles on r.fkProfileID equals p.luProfileID
join o in _database.tblOrganisations on r.fkOrgID equals o.pkOrgID
where r.fkUniqueID == intPKUserID
orderby o.OrgDesc, p.ProfileName
select new ProfileJSON
{
SiteID = o.pkOrgID,
SiteName = o.OrgDesc,
ProfileID = p.luProfileID,
ProfileName = p.ProfileName
}).Distinct().ToList(); //Distinct here