使用lambda表达式从一个列表中的一个列表中获取值

时间:2013-02-13 12:10:59

标签: c# linq lambda

我是lambda表达的新手,所以我试着解决一个问题。但我不能。所以有人可以为此提出解决方案。

我有一个班级客户。在里面我创建了另外3个类并为3个类创建了可观察的集合。我为这个客户创建了一个可观察的集合

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

        }

    }

我写了一个linq查询来获取来自customer2.anyhow的值的工作。就像我尝试编写一个lambda查询但我不能。

这里我将一些值添加到可观察集合中。

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

        }
            };

这是我的linq查询

  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

                      };

那么lambda query.pls会是什么意思。

2 个答案:

答案 0 :(得分:5)

使用问题where扩展的Matt解决方案将是:

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

答案 1 :(得分: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
                }
            )
        )
    )
);