LINQ Select from sub-list

You'll need to use SelectMany in order to access priceDiscounts:

var query = categoryProducts
            .SelectMany(x => x.productPrices)
            .SelectMany(y => y.priceDiscounts);

You need Enumerable.SelectMany

var result = categoryProducts.SelectMany(x => x.productPrices)
             .SelectMany(x => x.priceDiscounts);


  1. Jaiden

    • 2016/3/4

    You'll need to use SelectMany in order to access priceDiscounts : var query = categoryProducts .SelectMany(x => x.productPrices) .

  2. Rossetti

    • 2016/4/9

    LINQ Select from sub-list. Ask Question Asked 11 years, 10 months ago. Active 5 years, 10 months ago. Viewed 24k times 6 2. How can I make a Linq query to grab ALL

  3. Mitchell

    • 2018/12/20

    You can just use following LINQ expression: List1.Where(p => p.Cats.Any(c => List2.Any(c2 => c2.ID == c.ID)));.

  4. Marquis

    • 2018/11/9

    var c = from d in CustomerOrders select d.OrderLines.ToList(); IEnumerable<string> b = from q in (c as List<OrderLine>) select q.ProductCode; The first line is fine, but the next one causes an exception becase c is null. I didn't even get to trying to find the distinct values from the list :) Help and tips much appreciated! /Geir Rune

  5. Franklin

    • 2019/11/28

    Check this code: var query = from p in categories group p 

  6. Bruce

    • 2019/6/13

    int[] selectedRoles = GetSelectedRoles(); if( selectedRoles.Length > 0 ) { //somehow only select the userid from here ???: var subquery = from u in CompanyRolesToUsers where u.RoleID in selectedRoles select u.UserId; //somehow transform this into an Expression ???: var subExpression = Expression.Invoke(subquery); //and add it on to the existing expressions ???: predicateAnd = predicateAnd.And(subExpression); }

  7. Ariel

    • 2017/3/12

    Select(sides => sides.PositionList.Where(ps => ps.Position >= 1 && ps.Position <= 5))).ToList(); But 

  8. Matteo

    • 2015/1/5

    First, in the Select clause, I'll use a subquery to add a new item to the select list. This query adds the total sales in Canada to the information about each Customer in Canada: Select custId, totalPurchases, (Select Sum(totalValue) From Orders Where region = "Canada") As TotalCanadaSales From Customers Where region = "Canada"

  9. Mayson

    • 2018/12/16

    LINQ Select from sub-list. How can I make a Linq query to grab ALL Productpricediscounts from a category? public class ProductCategory { public 

  10. Guerra

    • 2015/12/13

    var query = from c in CustomerShip let subquery = from o in Orders where o.CustomerId == customerId && !o.OrderType.Equals("A") select o.OrderId from or in model.Orders where subquery.Contains(or.OrderId) && c.CustomerId == customerId && c.ShipSeq == or.CustShip && !or.OrderStatus.Equals("A") group c by c.ShipSeq into i select c.ShipSeq where c.CustomerId == customerId && !c.Address.Contains("RETAIL") && !c.Address.Contains("STORE") orderby c.ShipTo descending, c.OrderDate descending select

  11. Lukas

    • 2019/7/4

    C# answers related to “get sublist from list c# linq”. c# linq select from object list · c# get list of all class fields · get specific object in list c# 

  12. Marku

    • 2020/7/9

    If you want to select the students who are not from Computer Science department then you can use the following query: // Get the exam results of those students who are not in the Computer Science department var result = _examRepository.GetResults () .Where(p => GetStudents().All(p2 => p2.StudentID != p.StudentID)) .ToList();

  13. Harvey

    • 2021/4/22

    First, in the Select clause, I'll use a subquery to add a new item to the select list. This query adds the total sales in Canada to the 

  14. Sterling

    • 2020/6/18

    var obj = (from p in Person select new Person { Name= p.Name, ID= p.ID, Pet= p.Pet PetID=p.PetID }).ToList(); // I want to change the linq query to return the data with a sublist in the object, as below. I'm not sure if the below linq query would work properly. public class NewPerson { public string Name{get;set;} public int Id{get;set;} public List<Pets> pet{get;set;} } public class Pets { public string PetName{get;set;} public int PetID {get;set;} } var obj = (from p in Person select new

  15. Memphis

    • 2015/6/25

    Sub.Select(s => new { prop21 = s.C3 }) .ToList() // <-- Add this }); var res where prop2 is a List of anonymous type 'b which has a property prop21 .

  16. Brahimi

    • 2016/9/20

    I try to filter a list on property of a sub-sublist with linq, And to finish, my problem : I want to apply Linq query to list to obtain filtered_list.

  17. Rudy

    • 2019/3/31

    c# - I'm new to Linq but need to get some code finished quickly. I have two classes: public class SAPMember.

Comments are closed.

Recent Posts