Click here to Skip to main content
15,880,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi!

I'm trying to wrap my head around LINQ, but I find myself struggling here. I have a couple of very simple classes:

class Order
    {
        public string AccountNumber;
        public string AccountName;
        public List<OrderLine> OrderLines = new List<OrderLine>();
        public DateTime StartDate;
        public DateTime EndDate;       
    }

    class OrderLine
    {
        public string Description;
        public string ProductCode;
        public double Duration;
        public int Quantity;
    }


At run-time, I populate a new List object called customerOrders with instances of Orders, which again have multiple instances of OrderLine. Now, what I would like to do is to select the distinct ProductCode used in the entire customerOrder object. I have tried back and fort abit, and this is what I've come up with so far:
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
Posted

If you want to "flatten" enumerable of enumerables, the SelectMany extension method will do the trick.

var orders = new List<Order>();
var distinctOrderLines = orders.SelectMany(order => order.OrderLines).Distinct();
 
Share this answer
 
Comments
Magnus_ 27-Aug-10 4:46am    
orders.SelectMany(order => order.OrderLines).Select(orderLine => orderLine.ProductCode).Distinct()
Hi Friend,

According to my knowledge..here Iam giving a small and best solution for your problem

The below QueryExpression can solve your problem

//Step1: Get the OrderLines from Orders
var OrderLines = from pc1 in objOrder.OrderLines.ToList()
select pc1;

//step2: Get the Distinct ProductCodes
var productCodes = (from p1 in OrderLines
select new { p1.ProductCode }).Distinct();

//Step3: Bind the Dictinct ProductCodes to GridView
gvOrderLines.DataSource = productCodes;
gvOrderLines.DataBind();





I hope above code will help you.
All the Best...Happy Coding.

Best Regards,
Venkatesh velpula.
 
Share this answer
 
Have you tried doing it without the 'as' ? The whole point of var, I thought, was that you don't need to specify the type ? If you use the debugger, what IS the type of c ?
 
Share this answer
 
Hi, and thanks for taking the time to answer.
c is IEnumerable<list<orderline>> when I check the debugger. Hence I cannot access the ProductCode property like I try to do in the above code, only the Extension methods are available it seems.

/Geir Rune
 
Share this answer
 
Solved:

Hi and thanks so much for answering and trying to help me out. None of the answers so far actually solves my problem just by copy&paste though, so I haven't accepted any of the answers - sorry! :)

However, the following code selects distinct product codes from the above classes
var productCodes = from o in orders
                 from l in o.OrderLines
                 select l.ProductCode;
    productCodes = productCodes.Distinct();


    foreach (var code in productCodes)
    {
         Console.WriteLine(code);
    }


Thanks again!
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900