0

I have an SQL query which I need to convert to LINQ. have spent a whole day on it, didn't have much luck tough. Here's the query

SELECT PC.PRSCLAIMID,
date_format(PC.DATEOFCLAIM, '%d/%m/%Y') as DateofClaim,
PC.PRSREFNO,
PC.FKARTISTID,
PC.FKSETLISTINFOID,
PC.STATUS,
PC.FROMDNN,
PC.EXPORTTYPE,
PC.FK_CATEGORYID,
A.LOGINNAME,
A.BANDNAME,
SMCATEGORIESTYPE.CATEGORYTYPE,
Count(SMLIVECLAIMS.LIVECLAIMSID) AS GIGCOUNT,
SMCATEGORIES.ID AS PROCESSEDID,
SMCATEGORIES.CATEGORY,
Ifnull(BC.FILENAME, '') AS BULKCLAIM_NAME
FROM SMARTISTDETAILS AS A
INNER JOIN SMPRSCLAIMS AS PC
ON A.ARTISTID = PC.FKARTISTID
INNER JOIN SMCATEGORIES
ON PC.FK_CATEGORYID = SMCATEGORIES.ID
INNER JOIN SMCATEGORIESTYPE
ON SMCATEGORIES.FK_CATEGORYTYPEID = SMCATEGORIESTYPE.ID
INNER JOIN SMPRSLIVECLAIMLINK
ON PC.PRSCLAIMID = SMPRSLIVECLAIMLINK.FKPRSCLAIMID
INNER JOIN SMLIVECLAIMS
ON SMPRSLIVECLAIMLINK.FKLIVECLAIMID = SMLIVECLAIMS.LIVECLAIMSID
LEFT OUTER JOIN BULKCLAIMDETAIL BCD
ON BCD.FKPRSCLAIMID = PC.PRSCLAIMID
LEFT OUTER JOIN BULKCLAIM BC
ON BC.ID_BULKCLAIM = BCD.ID_BULKCLAIM
WHERE ( PC.EXPORTTYPE > 0 )
AND ( A.DBID = 1 )
AND ( CASE
WHEN PC.EXPORTTYPE = 1
AND ( SMLIVECLAIMS.GIGDATE < Date_add(Now(), INTERVAL -1 YEAR) ) THEN 0
ELSE 1
END = 1 )
GROUP BY PC.PRSCLAIMID
ORDER BY PC.DATEOFCLAIM DESC

what I did was the following conversion

    from e in
        (from artistData in dbContext.smartistdetails where artistData.DbId == 1 // SessionFacade.DBId
        join smprsclaimsData in dbContext.smprsclaims on artistData.ArtistID equals smprsclaimsData.fkArtistID join smCategoriesData in dbContext.smcategories on smprsclaimsData.FK_CategoryID equals smCategoriesData.Id join smCategoriesTypeData in dbContext.smcategoriestype on smCategoriesData.fk_CategoryTypeId equals smCategoriesTypeData.Id join smPrsLiveClaimsLinkData in dbContext.smprsliveclaimlink on smprsclaimsData.PRSClaimID equals smPrsLiveClaimsLinkData.fkPRSClaimID join smLiveClaimData in dbContext.smliveclaims on smPrsLiveClaimsLinkData.fkLiveClaimID equals smLiveClaimData.LiveclaimsID join bulkClaimDetailData in dbContext.bulkclaimdetail on smprsclaimsData.PRSClaimID equals bulkClaimDetailData.fkPRSClaimID into bulkClaimDetailJoined from bulkClaimDetailJoinedData in bulkClaimDetailJoined.DefaultIfEmpty() join bulkClaimData in dbContext.bulkclaim on bulkClaimDetailJoinedData.ID_BulkClaim equals bulkClaimData.ID_BulkClaim into bulkClaimJoined from bulkClaimJoinedData in bulkClaimJoined.DefaultIfEmpty() where smprsclaimsData.ExportType > 0 select new {
            PRSCLAIMID_Inner = smprsclaimsData.PRSClaimID,
            GigDate = smLiveClaimData.GigDate,
            DateofClaim = smprsclaimsData.DateofClaim,
            PRSREFNO = smprsclaimsData.PRSRefNo,
            FKARTISTID = smprsclaimsData.fkArtistID,
            FKSETLISTINFOID = smprsclaimsData.fkSetlistInfoID,
            STATUS = smprsclaimsData.Status,
            FROMDNN = smprsclaimsData.FromDNN,
            EXPORTTYPE = smprsclaimsData.ExportType,
            FK_CATEGORYID = smprsclaimsData.FK_CategoryID,
            LOGINNAME = artistData.LoginName,
            BANDNAME = artistData.BandName,
            CATEGORYTYPE = smCategoriesTypeData.CategoryType,
            LiveclaimsID = smLiveClaimData.LiveclaimsID,
            PROCESSEDID = smCategoriesData.Id,
            CATEGORY = smCategoriesData.Category,
            BULKCLAIM_NAME = bulkClaimJoinedData.FileName == null ? "" : bulkClaimJoinedData.FileName
        })
    where!(e.EXPORTTYPE == 1 && e.GigDate < oneYearOldDate)
    group e by e.PRSCLAIMID_Inner into groupedData
    select new {
        PRSCLAIMID_Outer = groupedData.Key,
        GIGCOUNT = groupedData.Count(),
        DateofClaim = groupedData.Select(a = > a.DateofClaim).FirstOrDefault(),
        PRSREFNO = groupedData.Select(a = > a.PRSREFNO).FirstOrDefault(),
        FKARTISTID = groupedData.Select(a = > a.FKARTISTID).FirstOrDefault(),
        FKSETLISTINFOID = groupedData.Select(a = > a.FKSETLISTINFOID).FirstOrDefault(),
        STATUS = groupedData.Select(a = > a.STATUS).FirstOrDefault(),
        FROMDNN = groupedData.Select(a = > a.FROMDNN).FirstOrDefault(),
        EXPORTTYPE = groupedData.Select(a = > a.EXPORTTYPE).FirstOrDefault(),
        FK_CATEGORYID = groupedData.Select(a = > a.FK_CATEGORYID).FirstOrDefault(),
        LOGINNAME = groupedData.Select(a = > a.LOGINNAME).FirstOrDefault(),
        BANDNAME = groupedData.Select(a = > a.BANDNAME).FirstOrDefault(),
        CATEGORYTYPE = groupedData.Select(a = > a.CATEGORYTYPE).FirstOrDefault(),
        PROCESSEDID = groupedData.Select(a = > a.PROCESSEDID).FirstOrDefault(),
        CATEGORY = groupedData.Select(a = > a.CATEGORY).FirstOrDefault(),
        BULKCLAIM_NAME = groupedData.Select(a = > a.BULKCLAIM_NAME).FirstOrDefault()
    }).OrderByDescending(a = > a.DateofClaim)

this conversion is working but it is too slow, then I create another conversion

from smprsclaimsData in dbContext.smprsclaims
group smprsclaimsData by new { smprsclaimsData.PRSClaimID, smprsclaimsData.fkArtistID, smprsclaimsData.FK_CategoryID } into smprsclaimsGroupedData
join artistData in dbContext.smartistdetails on smprsclaimsGroupedData.Key.fkArtistID equals artistData.ArtistID
let loginName = artistData.LoginName
let bandName = artistData.BandName
join smCategoriesData in dbContext.smcategories on smprsclaimsGroupedData.Key.FK_CategoryID equals smCategoriesData.Id
join smCategoriesTypeData in dbContext.smcategoriestype on smCategoriesData.fk_CategoryTypeId equals smCategoriesTypeData.Id
join smPrsLiveClaimsLinkData in dbContext.smprsliveclaimlink on smprsclaimsGroupedData.Key.PRSClaimID equals smPrsLiveClaimsLinkData.fkPRSClaimID
join smLiveClaimData in dbContext.smliveclaims on smPrsLiveClaimsLinkData.fkLiveClaimID equals smLiveClaimData.LiveclaimsID
join bulkClaimDetailData in dbContext.bulkclaimdetail on smprsclaimsGroupedData.Key.PRSClaimID equals bulkClaimDetailData.fkPRSClaimID into bulkClaimDetailJoined
from bulkClaimDetailJoinedData in bulkClaimDetailJoined.DefaultIfEmpty()
join bulkClaimData in dbContext.bulkclaim on bulkClaimDetailJoinedData.ID_BulkClaim equals bulkClaimData.ID_BulkClaim into bulkClaimJoined
from bulkClaimJoinedData in bulkClaimJoined.DefaultIfEmpty()
where smprsclaimsGroupedData.Select(a => a.ExportType).FirstOrDefault() > 0 && artistData.DbId == 1
&& !(smprsclaimsGroupedData.Select(a => a.ExportType).FirstOrDefault() == 1 && smLiveClaimData.GigDate < oneYearOldDate)
select new
{
PRSCLAIMID_Inner = 1,
GigDate = smLiveClaimData.GigDate,
DateofClaim = smprsclaimsGroupedData.Select(a => a.DateofClaim),
PRSREFNO = smprsclaimsGroupedData.Select(a => a.PRSRefNo),
FKARTISTID = smprsclaimsGroupedData.Select(a => a.fkArtistID),
FKSETLISTINFOID = smprsclaimsGroupedData.Select(a => a.fkSetlistInfoID),
STATUS = smprsclaimsGroupedData.Select(a => a.Status),
FROMDNN = smprsclaimsGroupedData.Select(a => a.FromDNN),
EXPORTTYPE = smprsclaimsGroupedData.Select(a => a.ExportType),
FK_CATEGORYID = smprsclaimsGroupedData.Select(a => a.FK_CategoryID),
LOGINNAME = loginName,
BANDNAME = bandName,
CATEGORYTYPE = smCategoriesTypeData.CategoryType,
//GIGCOUNT = smprsclaimsGroupedData.Count(),
PROCESSEDID = smCategoriesData.Id,
CATEGORY = smCategoriesData.Category,
BULKCLAIM_NAME = bulkClaimJoinedData.FileName == null ? "" : bulkClaimJoinedData.FileName,
})

well this query is not fetching the expected data, i think it is due to multiple (group by)'s. I cannot post my DB Schema as well, the question is already too long to read. Any help or any suggestions on how to get around or work around this conversion.

4

1 回答 1

1

我认为您对 LINQ 的想法有点过头了,请尝试以下 LINQ。我唯一不能 100% 确定的是 GIGCOUNT 结果,所以这可能需要一些摆弄。

(
from A in SMARTISTDETAILS

join PC in SMPRSCLAIMS on A.ARTISTID equals PC.FKARTISTID
join categories in SMCATEGORIES on PC.FK_CATEGORYID equals categories.ID
join categoryType in SMCATEGORIESTYPE on categories.FK_CATEGORYTYPEID equals categoryType.ID
join link in SMPRSLIVECLAIMLINK on PC.PRSCLAIMID equals link.FKPRSCLAIMID
join liveClaims in SMLIVECLAIMS on link.FKLIVECLAIMID equals liveClaims.LIVECLAIMSID

join bulkDetail in BULKCLAIMDETAIL on PC.PRSCLAIMID equals bulkDetail.FKPRSCLAIMID into BULK_DETAILS
from BCD in BULK_DETAILS.DefaultIfEmpty()

join bulkClaim in BULKCLAIM on BCD.ID_BULKCLAIM equals bulkClaim.ID_BULKCLAIM into BULK_CLAIMS
from BC in BULK_CLAIMS.DefaultIfEmpty()

where
   ( PC.EXPORTTYPE > 0 )
   && ( A.DBID == 1 )
   && (
      PC.EXPORTTYPE == 1 && liveClaims.GIGDATE < DateTime.Now.AddYears(-1) ? 0 : 1
   ) == 1

orderby PC.DATEOFCLAIM descending

select new
{
   PC.PRSCLAIMID,
   DateofClaim = PC.DATEOFCLAIM,
   PC.PRSREFNO,
   PC.FKARTISTID,
   PC.FKLISTINFOID,
   PC.STATUS,
   PC.FROMDNN,
   PC.EXPORTTYPE,
   PC.FK_CATEGORYID,
   A.LOGINNAME,
   A.BANDNAME,
   categoryType.CATEGORYTYPE,

   // This gets me the correct count now
   GIGCOUNT = SMPRSLIVECLAIMLINK.Where(LINK => LINK.FKPRSCLAIMID == PC.PRSCLAIMID && (PC.EXPORTTYPE == 1 && LINK.FKLIVECLAIMIDSMLIVECLAIMS.GIGDATE < DateTime.Now.AddYears(-1) ? 0 : 1) == 1).Count(),

   PROCESSEDID = categories.ID,
   categories.CATEGORY,
   BULKCLAIM_NAME = BC.FILENAME ?? String.Empty
})
// Use distinct to get rid of duplicate rows
.Distinct()
.ToList()
.GroupBy(o => 
   o.PRSCLAIMID
)

编辑:

我改变了计算 GIGCOUNT 属性的方式,现在应该返回正确的数字。您还需要使用 Distinct() 扩展,以便删除重复的行。

于 2013-07-21T20:55:06.923 回答