0

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

我有一类客户。在里面我创建了另外 3 个类并为 3 个类创建了 observable 集合。我为这个客户创建了一个 observable 集合

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 中获取值。无论如何它的工作原理。像这样我试图写一个 lambda 查询,但我不能。

在这里,我向 observable 集合添加了一些值。

  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 查询将是什么。请任何人建议。

4

2 回答 2

5

马特的解决方案从问题扩展where为:

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
                }
            )
        )
    )
);
于 2013-02-13T13:57:58.693 回答
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
                }
            )
        )
    )
);
于 2013-02-13T12:27:39.707 回答