Entity Framework Core take one and skip other duplicate item in a table

c# entity-framework-core linq

Question

I have a table that contains a list of string and many item in the table have the same string with a different ID, Now I want to take all the items from the table but not more than one items with the same string.

Example Table

id    -   string
------------------
 1   ---   A 
 2   ---   B 
 3   ---   B 
 4   ---   C 
 5   ---   D 
 6   ---   D 
 7   ---   F

after the query the list should look like

A, B, C, D, F

How can I do that using LINQ?

1
1
9/13/2018 8:54:24 PM

Accepted Answer

You could do something like this, I suppose:

var result = Context.yourTable.Select(t => t.fieldName).Distinct();

So, basically, your query is getting only distinct values for 'fieldName' (whatever that may be in your case).

1
9/13/2018 8:58:15 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow