Running a Distinct on a record [duplicate] - c#

This question already has answers here:
Distinct in Linq based on only one field of the table
(10 answers)
Closed 7 years ago.
In LINQ if the result that I have is the a list of whole records from the database and I want to do a Distinct on one column of this record, How do I do that?

You could try something as simple as:
var distincts = records.Select(x=>x.ColumnName).Distinct();

Related

Query a list and select top 10 values [duplicate]

This question already has answers here:
How to get first N elements of a list in C#?
(7 answers)
Closed 4 years ago.
I have a list of objects in which each object has a property called "Frequency" and I want to be able to pick the top 10 objects that have the highest frequencies.
I saw some solutions that are kind of similar to what I am looking to solve using LINQ so any help is appreciated.
You can order the list by descending Frequency and then take the first 10 like this:
var top10 = objectList.OrderByDescending(o => o.Frequency).Take(10);

Linq Distinct() is not working [duplicate]

This question already has answers here:
LINQ's Distinct() on a particular property
(23 answers)
Closed 8 years ago.
Distinct() is not working. It displays all the repeating values.
I searched for a solution but just got more confused. I tried this :
var categories = db.Orders.OrderBy(c => c.Item1).ToList().Distinct();
var categories = db.Orders.Distinct().OrderBy(c => c.Item1).ToList();
Is there a quick uncomplicated way to make this work?
use GroupBy instead of Distinct

Check a list has set of values [duplicate]

This question already has answers here:
Determine if a sequence contains all elements of another sequence using Linq [duplicate]
(4 answers)
Closed 8 years ago.
I have two sting lists
List<string> list1=new List(){"1","2","3"};
List<string> list2=new List(){"1","2"};
What will be the easiest way to check if list1 contains the values in list2.
How about
list1.Except(list2).Any();
Try using
[listName].Except(SecondListName).Any();
This should work.

IEnumreable and IQueryable [duplicate]

This question already has answers here:
What's the difference between IQueryable and IEnumerable [duplicate]
(6 answers)
Closed 8 years ago.
Proper difference of IEnumreable and IQueryable .
Why this two interface used and when used?
Whats the benefit used over list?
What other methods consist in IEnumreable and IQueryable?
In what scenario we have to use IEnumreable and IQueryable?
When querying a database, I prefer using a IQueryable because the request is sent when data are needed. I mean this
var users = (from user in db.Users select user ).AsQueryable();
//it doesn't load data yet until you write users.ToList(); or users.Count();

How can I remove all records from a table? [duplicate]

This question already has answers here:
Entity Framework. Delete all rows in table
(25 answers)
Closed 9 years ago.
I want to delete all records from my SQL Server table. I use LINQ to EF5. Is there anything I can do to do that?
context.Entities.DeleteAllOnSubmit(dc.Entities);

Categories