Click here to Skip to main content
11,707,254 members (48,896 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: .NET3.5 C# LINQ

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 Smile | :)

Help and tips much appreciated!

/Geir Rune
Posted 3-Dec-09 8:18am
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

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();
Partenon at 27-Aug-10 4:46am
orders.SelectMany(order => order.OrderLines).Select(orderLine => orderLine.ProductCode).Distinct()
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Hi Friend,

According to my 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;

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

Best Regards,
Venkatesh velpula.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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 ?
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Hi, and thanks for taking the time to answer.
c is IEnumerable> 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
Rate this: bad
Please Sign up or sign in to vote.

Solution 5


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! Smile | :)

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)

Thanks again!

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

  Print Answers RSS
0 OriginalGriff 141
1 Sergey Alexandrovich Kryukov 135
2 chainerlt 110
3 Maciej Los 80
4 ppolymorphe 79
0 OriginalGriff 9,178
1 Sergey Alexandrovich Kryukov 8,512
2 CPallini 5,189
3 Maciej Los 4,766
4 Mika Wendelius 3,696

Advertise | Privacy | Mobile
Web01 | 2.8.150819.1 | Last Updated 7 Dec 2009
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100