![]() ![]() #Ef select to new dto with icollections full#Say you need a result collection that will not only have the full list of Teams that match some criteria in all leagues, but need to know what League the teams are in. I look this extra result selector parameter as a helper object to help you know the relationship between the parent and child collections. The 3rd and 4th overloads of the SelectMany( ) method with the additional “result selector” parameter take a bit more explanation. Var players = leagues.SelectMany( l => l.Teams ) This most basic version gives you a flat collection of IEnumerable of all the teams in across all leagues. Now let’s write some queries (I use LINQPad to play with these queries and get immediate feedback/output). My collection of Leagues, Teams, and Players has a structure like the following: Well, this is exactly what SelectMany( ) can do for us. The “poor man’s” way to do this is use for-loops walk through each League, then each Team and collect the Players that I’m looking for. In this example, I want to find all the Players across all Leagues and Teams that meet some certain criteria. This is a fairly typical hierarchical collection of objects that I work with every day. ![]() Suppose I have defined some classes that represent a League that contains Teams and Teams contain Players. One of the extension methods in the class that I find I’m using more and more is the SelectMany( ) method – it wasn’t obvious at all to me when I first saw it what it’s purpose is. I have been using LINQ for a while now for pretty standard queryies, usually against object collections. ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |