LINQ - Nested Query

前端 未结 3 961
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-21 03:13

I have a SQL Statement that I am trying to convert to a LINQ query. I need to do this because I can\'t edit my database :(. Regardless, I have a SQL Statement that looks like th

3条回答
  •  半阙折子戏
    2021-01-21 03:26

    You can just nest the Linq to Entities query as well:

    var results = from x in context.MyEntities 
                  select new Customer() 
                  { 
                    CustomerID = x.CustomerID, 
                    FirstName = x.FirstName, 
                    LastName = x.LastName, 
                    Gender = x.Gender, 
                    BirthMonth = x.BirthMonth,
                    TotalPurchases = context.PurchaseOrders
                                            .Where(po=>po.CustomerId == x.CustomerId)
                                            .Count()
                  };
    

提交回复
热议问题