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();
Related
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:
Using Linq to group a list of objects into a new grouped list of list of objects
(5 answers)
Closed 4 years ago.
I got list of Issues,
Where Issue
public class Issue
{
public string version{ get; set; }
public string someDescription { get; set; }
}
versions are duplicated e.g:
List<
{"1.2.8", value1},
{"1.3.1", value2},
{"1.2.8", value3}
>
and I need to split duplicates into dictionary, where key is a specific verison, and value is a list of values like:
Dictionary<stirng, List<string>> duplicates;
what mean Dictionary<EachVersion, List<valuesForThisVersion>>
got something like this:
var VerisonList = records.GroupBy(r => r.Verison).Select(w=>w.Key).ToList();
this give me a list of versions, but have no idea how to fast get list of values where verison == VerisonList.element;
Is it possible with one linq?
EDIT: This is NOT a duplicate of the other question. In linked post there is no answer for my problem!!
Sure, using ToDictionary():
records.GroupBy(r => r.Verison)
.ToDictionary(g => g.Key,
g => g.Select(i => i.someDescription).ToList());
This question already has answers here:
Distinct not working with LINQ to Objects [duplicate]
(11 answers)
Closed 7 years ago.
I have a list of books, each book has a author assigned to it.
What I'm trying to do is allow the user the filter the results based on the author.
So some books may have the same Author etc but I want to filter the Authors out only show and entry for each one in my dropdown.
This is what I currently have :
var bookAuthors = Cache.CacheExtension.GetFromCache<List<BookCollection>>("books"); // Returns me the collection of books
// Filter the Authors out of the books
var authorCollection = bookAuthors.DistinctBy(item => new Options { Id = item.BookAuthor.ToString(), Description = item.BookAuthor }).ToList(); // Doesn't remove duplicates
As you can see I'm using DistinctBy but nothing gets filtered.
If I do this
var authorCollection = bookAuthors.Select(item => new Options { Id = item.BookAuthor.ToString(), Description = item.BookAuthor }).ToList(); // I get a list of Authors.
I get the list but it contains duplicates, any help would be grand
Use GroupBy() in Linq
var distinctItems = authorCollection.GroupBy(x => x.Id).Select(y => y.First());
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:
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.