What would be the nice way to make the following action:
List<IEnumerable<T>> listOfEnumerables = Get...();
List<T> listOfObjects = new List<T>();
// I want 'listOfObjects' to contain every element from every enumerable
// in 'listOfEnumerables'.
Is there any beautiful way to make this instead of the following:
foreach (var enumerable in listOfEnumerables)
{
listOfObjects.AddRange(enumerable);
}
Thank you.
You can use LINQ:
List<T> listOfObjects = listOfEnumerables.SelectMany(s => s).ToList();
listOfEnumerables.ForEach(i => listOfObjects.AddRange(i));
Related
I am trying to concate List<> as follows-
List<Student> finalList = new List<Student>();
var sortedDict = dictOfList.OrderBy(k => k.Key);
foreach (KeyValuePair<int, List<Student>> entry in sortedDict) {
List<Student> ListFromDict = (List<Student>)entry.Value;
finalList.Concat(ListFromDict);
}
But no concatenation happens. finalList remains empty. Any help?
A call to Concat does not modify the original list, instead it returns a new list - or to be totally accurate: it returns an IEnumerable<string> that will produce the contents of both lists concatenated, without modifying either of them.
You probably want to use AddRange which does what you want:
List<Student> ListFromDict = (List<Student>)entry.Value;
finalList.AddRange(ListFromDict);
Or even shorter (in one line of code):
finalList.AddRange((List<Student>)entry.Value);
And because entry.Value is already of type List<Student>, you can use just this:
finalList.AddRange(entry.Value);
Other answers have explained why Concat isn't helping you - but they've all kept your original loop. There's no need for that - LINQ has you covered:
List<Student> finalList = dictOfList.OrderBy(k => k.Key)
.SelectMany(pair => pair.Value)
.ToList();
To be clear, this replaces the whole of your existing code, not just the body of the loop.
Much simpler :) Whenever you find yourself using a foreach loop which does nothing but build another collection, it's worth seeing whether you can eliminate that loop using LINQ.
You may want to read up the documentation on Enumerable.Concat:
Return Value
Type: System.Collections.Generic.IEnumerable
An IEnumerable that contains the concatenated elements of the two input sequences.
So you may want to use the return value, which holds the new elements.
As an alternative, you can use List.AddRange, which Adds the elements of the specified collection to the end of the List.
As an aside, you can also achieve your goal with a simple LINQ query:
var finalList = dictOfList.OrderBy(k => k.Key)
.SelectMany(k => k.Value)
.ToList();
As specified here, Concat generates a new sequence whereas AddRange actually adds the elements to the list. You thus should rewrite it to:
List<Student> finalList = new List<Student>();
var sortedDict = dictOfList.OrderBy(k => k.Key);
foreach (KeyValuePair<int, List<Student>> entry in sortedDict) {
List<Student> ListFromDict = (List<Student>)entry.Value;
finalList.AddRange(ListFromDict);
}
Furthermore you can improve the efficiency a bit, by omitting the cast to a List<T> object since entry.Value is already a List<T> (and technically only needs to be an IEnumerable<T>):
var sortedDict = dictOfList.OrderBy(k => k.Key);
foreach (KeyValuePair<int, List<Student>> entry in sortedDict) {
finalList.AddRange(entry.Value);
}
Concat method does not modify original collection, instead it returns brand new collection with concatenation result. So, either try finalList = finalList.Concat(ListFromDict) or use AddRange method which modifies target list.
How do I refactor this in LINQ. I am working towards embracing good coding practices. My tds object looks like this:
tds -> BuildingName(List) -> buildingFloor(string)
What I am accomplishing with the nested foreach loop is reading the buildingFloors into a list.
List<string> bname = new List<string>();
foreach (var building in tds) {
foreach (var x in building.BuildingName) {
bname.Add(x);
}
}
You can do it with SelectMany which will flatten your items
List<string> bname = tds.SelectMany(b => b.BuildingName).ToList();
Use Enumerable.SelectMany to flatten your list like:
List<string> bname = tds.SelectMany(r=> r.BuildingName).ToList();
What you're looking for is SelectMany.
var bname = buildings.SelectMany(b => b.BuildingName);
Note, if you're going to be enumerating over the result multiple times with foreach then you will probably want to ToList() it, so you have a hard list rather than an enumerable that is executed every time you enumerate it.
var bname = buildings.SelectMany(b => b.BuildingName).ToList();
something like this I suppose:
var bname = tds.SelectMany(x => x.BuildingName);
And if you need a List call .ToList() at the end
var bname = tds.Select( x=> x.buildingName).ToList() ;
edit: I did not see the inner foreach while on my commute to work, it is indeed
var bname = tds.SelectMany(x=> x.BuildingName).ToList();
using query-syntax:
List<string> bname =
(from building in tds
from x in building.BuildingName
select x)
.ToList()
I have List<object> that seems above, I want to convert it to List<string>.
How can I convert it ?
I need List<string> that has 6 items (11:00,13:45,.... etc)
var mylist = myObjectList.ConvertAll(x => x.ToString());
Edit
var mylist = myObjectList.ConvertAll(x => Convert.ToString(x));
thanks Scott Chamberlain
To get first array of objects
var mylist = (myObjectList.First() as object[]).ToList()
.ConvertAll(x=>Convert.ToString(x));
To add rest to the list.
mylist.AddRange(mylist.GetRange(1,myObjectList.Count-2).ConvertAll(x=>Convert.ToString(x)));
var stringList = yourObjectList.OfType<string>().ToList();
Remember to add the namespace System.Linq;
The OfType is needed to convert the array to an array<T> which is necessary in order to use it with LINQ
Try this
List<string> stringlist = objectList.Cast<string>()
.ToList();
If you're not certain about those elements are strings you can use Select
List<string> stringlist = objectList.Select(x=> x.ToString())
.ToList();
To avoid NullReferenceException in case of null values try the following
List<string> stringlist = objectList.Where(x=> x != null)
.Select(x=> x.ToString())
.ToList();
Using LINQ this is fairly easy. If you are sure they are all strings you can simply do
int i = //Some code that sets i, like a for loop
var oldList = seanceInfo.theatre[i].seances;
List<string> newList = oldList.Cast<string>().ToList();
If you are not sure all of the objects are strings you need to perform some kind of conversion, however that is just as easy
List<string> newList = oldList.Select(o => Convert.ToString(o)).ToList();
From your comment: "seances is List<object>, and first index of seances is object[]. I need these items.", I think what you really want may be a SelectMany
List<string> info = seanceInfo.theatre.SelectMany(x => x.seances).Select(x => Convert.ToString(x)).ToList();
This will take each seance in each theater and combine it in to one master list.
You can simply cast it using LinQ.
myObjectList.Cast<string>();
Or filter all non-string
myObjectList.OfType<string>();
Casting like :
var list = (List<String>) listObjects.
I have two lists
List<T> list1 = new List<T>();
List<T> list2 = new List<T>();
I want remove all elements from list1, which also exist in list2. Of course I can loop through the first loop looking for each element in list2, but I am looking for elegant solution.
Thanks!
To change the actual list1 in place, you could use
list1.RemoveAll(item => list2.Contains(item));
You might instead prefer to simply have a query over the lists without modifying either
var result = list1.Except(list2);
LukeH makes a good recommendation in the comments. In the first version, and if list2 is particularly large, it might be worth it to load the list into a HashSet<T> prior to the RemoveAll invocation. If the list is small, don't worry about it. If you are unsure, test both ways and then you will know.
var theSet = new HashSet<YourType>(list2);
list1.RemoveAll(item => theSet.Contains(item));
With LINQ:
var result = list1.Except(list2);
list1.RemoveAll( item => list2.Contains(item));
Description
I think you mean the generic type List<Type>. You can use Linq to do this
Sample
List<string> l = new List<string>();
List<string> l2 = new List<string>();
l.Add("one");
l.Add("two");
l.Add("three");
l2.Add("one");
l2.Add("two");
l2.Add("three");
l2.Add("four");
l2.RemoveAll(x => l.Contains(x));
More Information
MSDN - List.RemoveAll Method
var result = list1.Except(list2);
Using LINQ you can do this:
List1.RemoveAll(i => !List2.Contains(i));
If you want to remove a list of objects (list2) from another list (list1) use:
list1 = list1.Except(list2).ToList()
Remember to use ToList() to convert IEnumerable<T> to List<T>.
var NewList = FirstList.Where(a => SecondList.Exists(b => b.ID != a.ID));
Using LINQ
Is there a way to "convert" (return) an IEnumerable list of, e.g., strings to an IEnumerable list of a different type when that different type accepts the former type in its constructor?
For example, the DataTable.Columns.AddRange() method accepts only lists of columns. Is there a way to return a DataColumn list by offering a string list using LINQ or some sort of aggregate function? I imagine the code would roughly do the following, but in one line:
var columnList = new List<DataColumn>();
foreach (var item in myStringList)
{
columnList.Add(item);
}
return columnList;
Likewise, is there an aggregate method that will take a list and run each of its members against a specific method? For example, I am looking for a one line way to perform the following similar foreach loop:
foreach (var item in myStringList)
{
myDataTable.Columns.Add(item);
}
Obviously, I am looking for generic answers that are not actually dependent on data columns or strings.
You can write
var newList = list.ConvertAll(x => new Something(x));
list.ForEach(x => DoSomething(x));
These methods are defined by th List<T> class.
If you have an arbitrary IEnumerable<T>, you can use LINQ:
var newEnumerable = enumerable.Select(x => new Something(x));
Call Enumerable.Aggregate
List<DataColumn> result = myStringList.Aggregate(
new List<DataColumn>(),
(list, item) => { list.Add(item); return list; }
);
return result;
That said, foreach statement is better.
Yes, in fact, although not all of them are LINQ specific. ForEach is just a List method. For your two examples:
myStringList.ForEach(x => columnList.Add(x));
// assumes myStringList is a List<T>... otherwise convert your enumerable using ToList()
The ForEach method takes an Action and lets you perform some logic on each item. So if you want to do transformations, it's easy enough combining with select:
myStringList.Select(x => new DataColumn(x))
.ToList()
.ForEach(x => columnList.Add(x));
// transforms each element of the string by adding some text, then calling foreach
// on the items
myStringList.ForEach(item => myDataTable.Columns.Add(item));
EDIT: that's not Linq. Sorry, my mistake.