1

I am using LINQ TO Entities & need to use Union operator.

This is my raw sql query.

(select DISTINCT c.DocumentId from [sDocument].[tDocumentStatus] c
inner join [sDocument].[tTOCStructure] d on  c.DocumentId = d.FolderID
inner join [sDocument].[tAudit] e on c.DocumentId = e.FolderID
where d.FolderType = 2 and d.isDeleted = 0 and d.ClientID = 9 and e.AuditDescriptionID != 10)
Union 
(select DISTINCT c.FolderID from [sDocument].[tTOCStructure] c 
inner join [sDocument].[tAudit] e on c.DocumentId = e.FolderID
where c.FolderType = 2 and c.isDeleted = 0 and c.ClientID = 9 )

When I run the above sql, I get around 45 records. That's right as well

Below is LINQ for the same requirement.

IQueryable<DocumentListMapper> query = (
from c in entities.tDocumentStatus
join d in entities.tTOCStructures on c.DocumentId equals d.FolderID
join e in entities.tAudits on c.DocumentId equals e.FolderID
where d.FolderType == 2 && d.isDeleted == false && d.ClientID == clientId && e.AuditDescriptionID != 10
select new DocumentListMapper()
{
DocumentId = c.DocumentId,
DocumentName = d.CheckoutFolderName,
PublishDate = c.AssignedDate
}).Distinct().Union(
from c in entities.tTOCStructures 
join e in entities.tAudits on c.FolderID equals e.FolderID
where c.FolderType == 2 && c.isDeleted == false && c.ClientID == clientId
select new DocumentListMapper()
{
DocumentId = c.FolderID,
DocumentName = c.CheckoutFolderName,
PublishDate = e.TaskDateTime
}).Distinct().OrderBy(x => x.PublishDate).Skip(pager * 50).Take(50);

But this LINQ returns more than 2500 records. This is not the desired records.

What's wrong in my LINQ??

Kgn-web
  • 7,047
  • 24
  • 95
  • 161
  • 2
    Notice that your distinct is run on the whole entry rather than on a single field as in your SQL. Distinct by certain field is not trivial, but can be done, see [this thread](http://stackoverflow.com/q/489258/728795) – Andrei Jan 05 '17 at 14:46
  • @juharr, how does that make such a big difference?? – Kgn-web Jan 05 '17 at 14:47
  • @Kgn-web Actually Andrei is right. Your sql query is only doing distinct on one column and your Linq is doing it on 3, so they are not the same at all. You could group by the `DocumentId` instead of using `Distinct`, but then you have to decide how to aggregate the other columns, like taking the first or last. – juharr Jan 05 '17 at 14:48
  • Also you should consider using the [navigation properties](https://coding.abel.nu/2012/06/dont-use-linqs-join-navigate/) that EF gives you instead of using joins to simplify your Linq query. – juharr Jan 05 '17 at 14:51
  • So what would be the SQL query that produces the same fields as your LINQ query? Currently you are comparing apples to oranges. – Ivan Stoev Jan 05 '17 at 14:59
  • I'd suggest to use SQL Profiler when running your C# application. Capture the query that's actually being ran in the database engine and compare it to your raw SQL query. – derloopkat Jan 05 '17 at 15:29
  • @derloopkat I am not sure how to use SQL profile with LINQ. Can you please share some details regarding this? Thanks :) – Kgn-web Jan 05 '17 at 15:39
  • Even if you never heard about it, you'll lean how to use it in 10 minutes. Look for a video tutorial in YouTube. For example https://www.youtube.com/watch?v=mJ8Dyv4Uk6E It's not rocket science, believe me. – derloopkat Jan 05 '17 at 16:18
  • @derloopkat, Thanks for the video:) – Kgn-web Jan 06 '17 at 05:14

0 Answers0