在linq中的一列上使用联接进行区分

在linq中的一列上使用联接进行区分,linq,Linq,我知道我们可以使用以下查询在一列上获得不同的结果: 我知道我们可以使用以下查询在一列上获得不同的结果: SELECT * FROM (SELECT A, B, C, ROW_NUMBER() OVER (PARTITION BY B ORDER BY A) AS RowNumber FROM MyTable WHERE B LIKE 'FOO%') AS a WHERE a.RowNumber = 1 在我连

我知道我们可以使用以下查询在一列上获得不同的结果: 我知道我们可以使用以下查询在一列上获得不同的结果:

SELECT  *
FROM    (SELECT A, B, C,
                ROW_NUMBER() OVER (PARTITION BY B ORDER BY A) AS RowNumber
         FROM   MyTable
         WHERE  B LIKE 'FOO%') AS a
WHERE   a.RowNumber = 1
在我连接多个表的情况下,我使用了类似的sql查询,但我的项目在mvc4中,我需要linq到实体的等价物。这是我的密码:

select * from
(
select fp.URN_No, 
ROW_NUMBER() OVER 
   (PARTITION BY pdh.ChangedOn ORDER BY fp.CreatedOn)
   as num,

fp.CreatedOn, pdh.FarmersName, pdh.ChangedOn, cdh.Address1,  cdh.State, ich.TypeOfCertificate, ich.IdentityNumber, bdh.bankType, bdh.bankName,
pidh.DistrictId,  pidh.PacsRegistrationNumber,  idh.IncomeLevel, idh.GrossAnnualIncome

from MST_FarmerProfile as fp inner join PersonalDetailsHistories as pdh on fp.personDetails_Id = pdh.PersonalDetails_Id
inner join ContactDetailsHistories as cdh on fp.contactDetails_Id = cdh.ContactDetails_Id
inner join IdentityCertificateHistories as ich on fp.IdentityCertificate_Id = ich.IdentityCertificate_Id
inner join BankDetailsHistories as bdh on fp.BankDetails_Id = bdh.BankDetails_Id
left join PacInsuranceDataHistories as pidh on fp.PacsInsuranceData_Id = pidh.PacsInsuranceData_Id
left join IncomeDetailsHistories as idh on fp.IncomeDetails_Id = idh.IncomeDetails_Id

where URN_No in(
select distinct MST_FarmerProfile_URN_No from PersonalDetailsHistories where MST_FarmerProfile_URN_No in(
select URN_No from MST_FarmerProfile where (CreatedOn>=@fromDate and CreatedOn<= @toDate and Status='Active')))
)a where a.num=1

从sql获取结果后使用此linq查询。p、 ID是您想要的唯一列名

List<Person> distinctRecords = YourResultList
            .GroupBy(p => new { p.ID})
            .Select(g => g.First())
            .ToList();