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 .

4
  • Here Nixex09 if you are learning Lambda's then start reading here msdn.microsoft.com/en-us/library/bb397687.aspx Commented Feb 13, 2013 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? Commented Feb 13, 2013 at 12:15
  • possible duplicate of convert this LINQ expression into Lambda Commented Feb 13, 2013 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. Commented Feb 13, 2013 at 14:42

2 Answers 2

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
                }
            )
        )
    )
);
Sign up to request clarification or add additional context in comments.

Comments

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
                }
            )
        )
    )
);

2 Comments

thanks...u got my question.this is just learning purpose only..anyway u save my lot of time..
how we can add 'where' condition in this expression

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.