0

i am new to lambda expression so i try to solve one problem .but i can't. so can anyone suggest solution for this.

i have one class customer. inside i created another 3 class and create observable collection for 3 classes.i create one observable collection for this customer

ObservableCollection<Customer> customer2;

   public class Customer
    {
        public string CusName { get; set; }
        public int CusAge { get; set; }
        public ObservableCollection<Bankdetails> bankdetails;
        public ObservableCollection<order> orderlist;
        public ObservableCollection<orderdetails> orderdetailslist;

        public class Bankdetails
        {
            public string Bankaccno { get; set; }
            public string bankname { get; set; }
            public int bankid { get; set; }
        }

        public class order
        {
            public string ordername { get; set; }
            public string orderid { get; set; }

        }

        public class orderdetails
        {
            public string orderid { get; set; }
            public string itemname { get; set; }
            public int itemqty { get; set; }

        }

    }

i write one linq query for getting values from customer2.anyhow its working .like this i tried to write one lambda query but i can't.

here i adding some values to observable collection.

  customer2 = new ObservableCollection<Customer>
        {
            new Customer()
        {
            CusName="nixon",CusAge=24,
            bankdetails=new ObservableCollection<Customer.Bankdetails>
            {
                new Customer.Bankdetails()
                {
                    bankid=12,bankname="axis",Bankaccno="09876534"
                }
            },
            orderlist=new ObservableCollection<Customer.order>
            {
                new Customer.order
                {
                    orderid="Od123",ordername="Express"
                }
            },
            orderdetailslist=new ObservableCollection<Customer.orderdetails>
            {
                new Customer.orderdetails
                {
                    orderid="Od123",itemname="cpu",itemqty=5
                }
            }

        }
            };

this is my linq query

  var customer1 = from cus in customer2
                      from bank in cus.bankdetails
                      from ord in cus.orderlist
                      from orddet in cus.orderdetailslist
                      where ord.orderid == orddet.orderid 

                      select new
                      {
                          cus.CusAge,cus.CusName,
                          bank.Bankaccno,bank.bankid,bank.bankname,
                          ord.ordername,
                          orddet.itemname,orddet.itemqty

                      };

then what will be the lambda query.pls anyone suggest .

svick
  • 236,525
  • 50
  • 385
  • 514
nichu09
  • 882
  • 2
  • 15
  • 44
  • Here `Nixex09` if you are learning Lambda's then start reading here http://msdn.microsoft.com/en-us/library/bb397687.aspx – MethodMan Feb 13 '13 at 12:15
  • I really don't understand the question. What exactly do you want to do? Why doesn't the query you wrote work for you? What results do you want? What results do you actually get? – svick Feb 13 '13 at 12:15
  • possible duplicate of [convert this LINQ expression into Lambda](http://stackoverflow.com/questions/1524813/convert-this-linq-expression-into-lambda) – MethodMan Feb 13 '13 at 12:17
  • As you are learning LINQ, try using LINQPad. One of it's features is the ability to show you the Lambda version of your query expression. – Jim Wooley Feb 13 '13 at 14:42

2 Answers2

5

Matt's solution extended with the where from the question would be:

var xxx = customer2.SelectMany(cus =>
    cus.bankdetails.SelectMany(bank => 
        cus.orderlist.SelectMany(ord => 
           cus.orderdetailslist.Where(orddet => orddet.orderid == ord.orderid)
              .Select(orddet => new
                {
                    cus.CusAge,
                    cus.CusName,
                    bank.Bankaccno,
                    bank.bankname,
                    orddet.itemname,
                    orddet.itemqty
                }
            )
        )
    )
);
ykb
  • 221
  • 2
  • 11
3
var xxx = customer2.SelectMany(cus =>
    cus.bankdetails.SelectMany(bank => 
        cus.orderlist.SelectMany(ord => 
            cus.orderdetailslist.Select(orddet => new
                {
                    cus.CusAge,
                    cus.CusName,
                    bank.Bankaccno,
                    bank.bankname,
                    orddet.itemname,
                    orddet.itemqty
                }
            )
        )
    )
);
Matt Whetton
  • 6,616
  • 5
  • 36
  • 57