我有一个返回数据的LINQ查询,我想基于2个字段(fkProfileID和fkOrgID)进行过滤,这些字段通常是相同的。当fkProfileID和fkOrgID匹配时,我只想显示1条记录,因此它类似于执行SQL distinct,但在2个字段上而不是一个。
我的数据看起来像这样(其他字段已被删除):
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();
您可以在查询后使用。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