All Questions

Tagged with
Filter by
Sorted by
Tagged with
861 votes
11 answers
668k views

IEnumerable vs List - What to Use? How do they work?

I have some doubts over how Enumerators work, and LINQ. Consider these two simple selects: List<Animal> sel = (from animal in Animals join race in Species ...
Axonn's user avatar
  • 10.2k
779 votes
14 answers
861k views

Using LINQ to remove elements from a List<T>

Say that I have LINQ query such as: var authors = from x in authorsList where x.firstname == "Bob" select x; Given that authorsList is of type List<Author>, how can ...
TK.'s user avatar
  • 47.1k
525 votes
13 answers
513k views

C# LINQ find duplicates in List

Using LINQ, from a List<int>, how can I retrieve a list that contains entries repeated more than once and their values?
Mirko Arcese's user avatar
  • 5,323
403 votes
5 answers
174k views

Flatten List in LINQ

I have a LINQ query which returns IEnumerable<List<int>> but i want to return only List<int> so i want to merge all my record in my IEnumerable<List<int>> to only one ...
Cédric Boivin's user avatar
390 votes
11 answers
412k views

Remove duplicates in the list using linq

I have a class Items with properties (Id, Name, Code, Price). The List of Items is populated with duplicated items. For ex.: 1 Item1 IT00001 $100 2 Item2 ...
Prasad's user avatar
  • 59.2k
324 votes
18 answers
595k views

Quickest way to compare two generic lists for differences

What is the quickest (and least resource intensive) to compare two massive (>50.000 items) and as a result have two lists like the ones below: items that show up in the first list but not in the ...
Frank's user avatar
  • 4,009
289 votes
4 answers
821k views

Select distinct using linq [duplicate]

I have a class list of class public class LinqTest { public int id { get; set; } public string value { get; set; } } List<LinqTest> myList = new List<LinqTest>(); myList.Add(new LinqTest(...
Anoop Joshi P's user avatar
254 votes
6 answers
660k views

Get a list of distinct values in List

In C#, say I have a class called Note with three string member variables. public class Note { public string Title; public string Author; public string Text; } And I have a list of type ...
Darrel Hoffman's user avatar
228 votes
5 answers
454k views

Linq select objects in list where exists IN (A,B,C)

I have a list of orders. I want to select orders based on a set of order statuses. So essentially select orders where order.StatusCode in ("A", "B", "C") // Filter the orders based on the order ...
MartinS's user avatar
  • 6,164
200 votes
13 answers
860k views

Check if list contains element that contains a string and get that element

While searching for an answer to this question, I've run into similar ones utilizing LINQ but I haven't been able to fully understand them (and thus, implement them), as I'm not familiarized with it. ...
Dimitris Iliadis's user avatar
191 votes
5 answers
153k views

How do I use LINQ to obtain a unique list of properties from a list of objects?

I'm trying to use LINQ to return a list of ids given a list of objects where the id is a property. I'd like to be able to do this without looping through each object and pulling out the unique ids ...
mezoid's user avatar
  • 28.4k
189 votes
6 answers
281k views

LINQ: "contains" and a Lambda query

I have a List<BuildingStatus> called buildingStatus. I'd like to check whether it contains a status whose char code (returned by GetCharCode()) equals some variable, v.Status. Is there some way ...
mark smith's user avatar
  • 20.8k
169 votes
10 answers
65k views

Check whether an array is a subset of another

Any idea on how to check whether that list is a subset of another? Specifically, I have List<double> t1 = new List<double> { 1, 3, 5 }; List<double> t2 = new List<double> { 1,...
Graviton's user avatar
  • 82.6k
150 votes
9 answers
533k views

how to check if object already exists in a list

I have a list List<MyObject> myList and I am adding items to a list and I want to check if that object is already in the list. so before I do this: myList.Add(nextObject); I want to ...
leora's user avatar
  • 192k
142 votes
6 answers
113k views

C#: How to convert a list of objects to a list of a single property of that object?

Say I have: IList<Person> people = new List<Person>(); And the person object has properties like FirstName, LastName, and Gender. How can I convert this to a list of properties of the ...
User's user avatar
  • 64.1k
142 votes
4 answers
202k views

Lists: Count vs Count() [duplicate]

Given a list, which method is preferred to determine the number of elements inside? var myList = new List<string>(); myList.Count myList.Count()
Saxman's user avatar
  • 5,039
138 votes
2 answers
134k views

Easier way to populate a list with integers in .NET [duplicate]

Possible Duplicate: Populating a list of integers in .NET Is there a simpler or more elegant way of initializing a list of integers in C# other than this? List<int> numberList = new List&...
user avatar
134 votes
10 answers
103k views

How can I get every nth item from a List<T>?

I'm using .NET 3.5 and would like to be able to obtain every *n*th item from a List. I'm not bothered as to whether it's achieved using a lambda expression or LINQ. Edit Looks like this question ...
Paul Suart's user avatar
  • 6,573
130 votes
16 answers
163k views

Checking if a list is empty with LINQ

What's the "best" (taking both speed and readability into account) way to determine if a list is empty? Even if the list is of type IEnumerable<T> and doesn't have a Count property. Right now I'...
Matt Hamilton's user avatar
124 votes
3 answers
62k views

.NET List<T> Concat vs AddRange

What is the difference between the AddRange and Concat functions on a generic List? Is one recommended over the other?
johnc's user avatar
  • 39.8k
108 votes
10 answers
50k views

Count property vs Count() method?

Working with a collection I have the two ways of getting the count of objects; Count (the property) and Count() (the method). Does anyone know what the key differences are? I might be wrong, but I ...
user avatar
107 votes
7 answers
25k views

Does Distinct() method keep original ordering of sequence intact?

I want to remove duplicates from list, without changing order of unique elements in the list. Jon Skeet & others have suggested to use the following: list = list.Distinct().ToList(); Reference: ...
Nitesh's user avatar
  • 2,740
106 votes
6 answers
167k views

Freely convert between List<T> and IEnumerable<T>

How can I convert a List<MyObject> to an IEnumerable<MyObject> and then back again? I want to do this in order to run a series of LINQ statements on the List, e. g. Sort()
TK.'s user avatar
  • 47.1k
103 votes
10 answers
164k views

C# Determine if List Has Duplicate

Requirement: In an unsorted List, determine if a duplicate exists. The typical way I would do this is an n-squared nested loop. I'm wondering how others solve this. Is there an elegant, high ...
kakridge's user avatar
  • 2,243
101 votes
7 answers
265k views

Sorting a List<int>

Using C# what is the best way to sort a List numerically? my list has items 5,7,3 and I would like them sorted 3,5,7. I know some longer ways, but I would imagine linq has a quicker way? sorry this ...
Spooks's user avatar
  • 7,077
92 votes
8 answers
175k views

Conversion from List<T> to array T[]

Is there a short way of converting a strongly typed List<T> to an Array of the same type, e.g.: List<MyClass> to MyClass[]? By short i mean one method call, or at least shorter than: ...
tehvan's user avatar
  • 10.3k
91 votes
5 answers
149k views

Simplest way to form a union of two lists

What is the easiest way to compare the elements of two lists say A and B with one another, and add the elements which are present in B to A only if they are not present in A? To illustrate, Take list ...
RSK's user avatar
  • 2,142
90 votes
8 answers
158k views

How to check list A contains any value from list B?

List A: 1, 2, 3, 4 List B: 2, 5 How to check if list A contains any value from list B? e.g. something like A.contains(a=>a.id = B.id)?
wahaha's user avatar
  • 925
85 votes
7 answers
144k views

How to get a distinct list from a List of objects?

I have a List<MyClass> someList. class MyClass { public int Prop1... public int Prop2... public int Prop3... } I would like to know how to get a new distinct List<MyClass> ...
Willem's user avatar
  • 9,346
82 votes
11 answers
103k views

How to Quickly Remove Items From a List

I am looking for a way to quickly remove items from a C# List<T>. The documentation states that the List.Remove() and List.RemoveAt() operations are both O(n) List.Remove List.RemoveAt This ...
user807566's user avatar
  • 2,858
77 votes
17 answers
31k views

Easiest way to Rotate a List in c#

Lists say I have a list List<int> {1,2,3,4,5} Rotate means: => {2,3,4,5,1} => {3,4,5,1,2} => {4,5,1,2,3} Maybe rotate is not the best word for this, but hope you understand what I ...
Eric Yin's user avatar
  • 8,853
76 votes
9 answers
412k views

How to get first object out from List<Object> using Linq

I have below code in c# 4.0. //Dictionary object with Key as string and Value as List of Component type object Dictionary<String, List<Component>> dic = new Dictionary<String, List<...
Manoj Singh's user avatar
  • 7,659
75 votes
6 answers
64k views

How to get the closest number from a List<int> with LINQ?

How to get the closest number from a List<int> with LINQ? For example: List<int> numbers = new List<int>(); numbers.Add(2); numbers.Add(5); numbers.Add(7); numbers.Add(10) I need ...
ale's user avatar
  • 3,381
75 votes
6 answers
40k views

Sort one list by another

I have 2 list objects, one is just a list of ints, the other is a list of objects but the objects has an ID property. What i want to do is sort the list of objects by its ID in the same sort order as ...
JGilmartin's user avatar
  • 8,981
74 votes
5 answers
107k views

How to find List has duplicate values in List<string> [duplicate]

How to find whether the List<string> has duplicate values or not ? I tried with below code. Is there any best way to achieve ? var lstNames = new List<string> { "A", "B", "A" }; if (...
Prasad Kanaparthi's user avatar
73 votes
5 answers
81k views

How to select values within a provided index range from a List using LINQ

I am a LINQ newbie trying to use it to acheive the following: I have a list of ints:- List<int> intList = new List<int>(new int[]{1,2,3,3,2,1}); Now, I want to compare the sum of the ...
Punit Vora's user avatar
  • 5,116
72 votes
3 answers
61k views

Differences between IQueryable, List, IEnumerator?

I am wondering what the difference between IQueryable, List, IEnumerator is and when I should use each one? For instance when using Linq to SQL I would do something like this: public List<User>...
chobo2's user avatar
  • 84.3k
72 votes
5 answers
46k views

C# Difference between First() and Find()

So I know that Find() is only a List<T> method, whereas First() is an extension for any IEnumerable<T>. I also know that First() will return the first element if no parameter is passed, ...
Squirrelsama's user avatar
  • 5,500
61 votes
3 answers
60k views

LINQ: How to skip one then take the rest of a sequence

i would like to iterate over the items of a List<T>, except the first, preserving the order. Is there an elegant way to do it with LINQ using a statement like: foreach (var item in list.Skip(...
Marcel's user avatar
  • 15.4k
60 votes
5 answers
108k views

Remove items of list from another lists with criteria

i have a list of writers. public class Writers{ long WriterID { get;set; } } Also I have two lists of type Article. public class Article{ long ArticleID { get; set; } long WriterID { ...
developer's user avatar
  • 3,841
59 votes
6 answers
128k views

Compare Two Lists Via One Property Using LINQ

Say I have the following: class Widget1{ public int TypeID { get; set; } public string Color { get; set; } } class Widget2 { public int TypeID { get; set; } ...
Coltech's user avatar
  • 1,690
56 votes
5 answers
93k views

Using LINQ to convert List<U> to List<T>

I have 2 classes which have some identical properties. I stock into a list properties from 1st class, and after that, I want to take some needed properties and put them into a list of 2nd class type. ...
mihai's user avatar
  • 2,766
55 votes
10 answers
32k views

Why is .ForEach() on IList<T> and not on IEnumerable<T>? [duplicate]

Possible Duplicate: Why is there not a ForEach extension method on the IEnumerable interface? I've noticed when writing LINQ-y code that .ForEach() is a nice idiom to use. For example, here is a ...
Olema's user avatar
  • 561
46 votes
12 answers
155k views

compare two list and return not matching items using linq

i have a two list List<Sent> SentList; List<Messages> MsgList; both have the same property called MsgID; MsgList SentList MsgID Content MsgID Content Stauts 1 ...
Spen D's user avatar
  • 4,305
46 votes
2 answers
29k views

populate a dictionary using linq

I have the following empty Dictionary Dictionary<Guid, string> appTypeMap = new Dictionary<Guid, string>(); and the following list: List<ApplicationType> allApplicationTypes =...
Bick's user avatar
  • 18.1k
43 votes
2 answers
62k views

Get index of object in a list using Linq [duplicate]

I am new to Linq. I have a Customers table.ID,FullName,Organization,Location being the columns. I have a query in Sqlite returning me 2500 records of customers. I have to find the index of the ...
RookieAppler's user avatar
  • 1,527
42 votes
4 answers
57k views

Remove all empty elements from string array

I have this: List<string> s = new List<string>{"", "a", "", "b", "", "c"}; I want to remove all the empty elements ("") from it quickly (probably through LINQ) without using a foreach ...
Elmo's user avatar
  • 6,433
40 votes
7 answers
95k views

Can I select multiple objects in a Linq query

Can I return more than one item in a select? For instance I have a List of Fixtures (think football (or soccer for the yanks) fixtures). Each fixture contains a home and away team and a home and away ...
James Hay's user avatar
  • 12.6k
40 votes
4 answers
65k views

Get the difference between two lists using LINQ

I have two lists and I need to compare them and only return a List of Items not in both. var listOfIds = new List<int> {1,2,4}; var persons = new ObservableCollection<Person> { new ...
gulbaek's user avatar
  • 2,521
39 votes
4 answers
91k views

Can LINQ ForEach have if statement?

Is it possible to add if-statement inside LINQ ForEach call? sequence.Where(x => x.Name.ToString().Equals("Apple")) .ToList() .ForEach( /* If statement here */ );
abc cba's user avatar
  • 2,593

1
2 3 4 5
58