This question already has answers here:
Sorting ObservableCollection [duplicate]
(4 answers)
Closed 9 years ago.
I've tried:
Persons = from i in Persons orderby i.Age select i;
But I cant convert Linqs System.Linq.IOrderedEnumerable to ObservableCollection<Person>.
You just need to create a new instance of it.
Persons = new ObservableCollection<Person>(from i in Persons orderby i.Age select i);
An ObservableCollection can take in an IEnumerable<T> (i.e, in this instance, your IOrderedEnumerable) from it's constructor:
http://msdn.microsoft.com/en-us/library/cc679169.aspx
You might want to simply create a new ObservableCollection from the sorted enumerable.
Related
This question already has answers here:
Using LINQ, select list of objects inside another list of objects
(2 answers)
Closed 1 year ago.
I am trying to extract a list of Goals from within a list of Students using this method
public List<Goal> GetGoalsForTeacher(int userId)
{
var students = GetStudentsForTeacher(userId);
var result = students.Select(e => e.Goals).ToList();
return result
}
However, I need it to return a List, but it is returning a List<IList>. Has anyone any idea on how I would convert this or make it a List initially?
You're looking for the SelectMany() method.
var result = students.SelectMany(e => e.Goals).ToList();
This question already has answers here:
Multiple "order by" in LINQ
(7 answers)
Closed 3 years ago.
I'm trying to order by WeekId, then Order in my SQL table (end result should have workouts together by id, then ordered by the order specified), yet it is giving the wrong order. Is there something wrong with my LINQ statement?
private List<Workout> GetWorkouts(int id)
{
return new OPPDBContext().Workouts
.Where(p=>p.ClientId == id).OrderBy(p => p.Order).OrderBy(p => p.WeekId).ToList();
}
The table:
The results:
Expected results:
Lat Pulldowns
Squats
Lat Pulldowns
Squats
Reverse Lunges
That's because the second .OrderBy replaces the first .OrderBy (you are sorting by ClientId, and then effectively discard that to sort by WeekId).
You need to use .OrderBy(...).ThenBy(...) instead:
return new OPPDBContext().Workouts.Where(p=>p.ClientId == id).OrderBy(p => p.Order).ThenBy(p => p.WeekId).ToList();
OrderBy docs
ThenBy docs
This question already has answers here:
Multiple "order by" in LINQ
(7 answers)
Closed 4 years ago.
I want to sort the collection before processing. I used to use this method:
foreach (var item in Items.OrderBy(i => i.property1))
{
...
}
Now i need to sort it by property2, if property values is equals between two items. Is there any method to do that logic via LINQ expression?
Is this what you're after?
foreach (var item in Items.OrderBy(i => i.property1).ThenBy(i => i.property2))
{
...
}
This question already has answers here:
Using LINQ to remove elements from a List<T>
(14 answers)
Closed 9 years ago.
I have a List of type some entity
List
public class OrderLine
{
public string productCode;
public int quantity;
}
i need to remove items from the above List if the productCode is equal to some products.
List<string> ProductsToBeExcluded = new List<string>(){"1234","1237"};
so, from List<OrderLine> i need to remove products which are equal to 1234 and 1237
i have tried
create a List<string> from List<OrderLine> using
List<OrderLine> OrderLines = GetOrderLines();
var ol = from o in OrderLines
select o.ProductCode;
2.
List<string> ProductsToBeExcluded = new List<string>(){"1234","1237"};
var filtered = OrderLines.Except(ProductsToBeExcluded);
how do I proceed further in removing
thanks
In this case you don't need LINQ but can just use List<T>.RemoveAll instead
OrderLines.RemoveAll(x => ProductsToBeExcluded.Contains(x.ProductCode));
Use RemoveAll method of List which accepts predicate
OrderLines.RemoveAll(x => ProductsToBeExcluded.Contains(x.ProductCode));
This question already has answers here:
LINQ Select Distinct with Anonymous Types
(8 answers)
Closed 9 years ago.
How does Distinct() work on a List<> of anonymous type? will it just do a property compare? or will it always return the same list?
example:
List<SomeObject> list;
....
....
var result = list
.Where(i => i.Condition)
.Select(i => new
{
Name = i.Name,
Date = i.Date
});
.Distinct()
.ToList()
Please note I applied the distinct on the anonymous-type list.
From MSDN
Because the Equals and GetHashCode methods on anonymous types are defined in terms of the Equals and GetHashcode methods of the properties, two instances of the same anonymous type are equal only if all their properties are equal.