Click here to Skip to main content
15,881,139 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi -

I am using entity framework linq query to retrieve my data. Problem is I am getting duplicate rows of invoice number. How do I eliminate the duplicate rows.
Invoice Number is duplicating for invoices that have multiple invoice items.

Here is my query

C#
result = (from invoice in invoices
          join invoiceItem in invItems on invoice.Id equals invoiceItem.InvoiceId
          orderby invoice.InvoiceNo
          select new InvoiceReceiveShipmentVM
          {
              dtInvoiced = invoice.dtInvoiced,
              InvoiceNumber = invoice.InvoiceNo,
              InvoiceType = invoice.InvoiceType,
              InvoiceStatus = invoice.InvoiceStatus,
              Lines = invoiceItem.Line,
              Total = invoice.Total,
              Carrier = invoice.Carrier,
          });
return result.Distinct().AsQueryable();


What I have tried:

I've tried result.Distinct().ToList() and I've been looking through at this link to find something http://linq101.nilzorblog.com/linq101-lambda.php
Posted
Updated 23-Jun-16 15:54pm
Comments
Ehsan Sajjad 23-Jun-16 16:40pm    
that is the expected behaviour you have multiple items against a invoice number, you need to write logic at front end for displaying it one time may be grouping them
Maciej Los 23-Jun-16 17:39pm    
Well, imagine that: there's one invoice with these properties: ID - 5, No.: 5; and second one: ID - 6, No.: 5. Are they equal?
Beginner Luck 23-Jun-16 21:38pm    
you want to Distinct by which column
Troy Bryant 23-Jun-16 21:45pm    
InvoiceNumber
Beginner Luck 23-Jun-16 21:55pm    
I not sure is this wat you need

1 solution

C#
public static IEnumerable DistinctBy(this IEnumerable list, Func<t, object> propertySelector)

{

   return list.GroupBy(propertySelector).Select(x => x.First());

}


Try this useful extension

from this website
LINQ DistinctBy - With Lambda Expression Parameter - Eleven Winds[^]
 
Share this answer
 
Comments
Maciej Los 7-Jul-16 2:29am    
Nice! A5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900