0

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

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
4

1 回答 1

2

从 sql 获取结果后使用此 linq 查询。p.ID 是您想要的不同列名

List<Person> distinctRecords = YourResultList
            .GroupBy(p => new { p.ID})
            .Select(g => g.First())
            .ToList();
于 2013-11-15T07:30:52.450 回答