我如何在Select中使用LINQ Select查找总价格

时间:2019-01-30 18:57:53

标签: c# winforms linq select

enter image description here

正如您在所附图像中看到的那样,当我按客户ID搜索数据时,我想显示另一个具有相同ReservationId的表的总价。 通过ReservId进行搜索非常容易,并且可以像这样正常工作。

    db = new RoomEntities();
    int ReservId = Convert.ToInt32(txtReservId.Text.Trim());
    var BarPrice = (from u in db.Transactions
    where u.ReservationId == ReservId  
    group u by u.ReservationId into g
    select new
    {
      Total = g.Sum(x => x.TotalPrice) // I will use this to assign in dataGridView of Reservation
    }).FirstOrDefault();

   var SeachByReservationId = (from u in db.Reservation
                               join c in db.Customer on u.CustomerId equals c.CustomerId
                              join r in db.Room on u.RoomId equals r.RoomId
                               where u.ReservationId == ReservId
                               select new
                               {
                                ReservationId = u.ReservationId,
                                RoomNr = r.RumNummer,
                                Name = c.FirstName+" "+c.LastName,
                                IdCard = c.IdCardNr,
                                StartDate = u.StartDate,
                                EndDate = u.EndDate,
                                Totaldays = u.TotalDays,
                                TotalRoomprice = u.TotalPrice,
        // Then I assign BarPrice.Total to TotalBarPrice
                 TotalBarPrice = Convert.ToDecimal(BarPrice.Total),
                                    }
                                ).ToList();
                        dgvReservation.DataSource = SeachByReservationId;

但是,如果我想按客户ID进行搜索,那么它将是多行,我的意思是如果客户之前多次预订,则具有相同客户ID但具有不同ReservationId的多个结果。 就像同一位客户的旧预订一样。然后,我不知道如何从“交易”表中得出恐龙和食物的总价。我尝试这样,但不起作用。请帮助..

(from u in db.Reservation
 join c in db.Customer on u.CustomerId equals c.CustomerId
 join r in db.Room on u.RoomId equals r.RoomId
 join tr in db.Transactions on u.ReservationId equals tr.TransactionId
 where u.CustomerId == custId.CustomerId
 select new
 {
   ReservationId = u.ReservationId,
   ...... .....
  // like this but not working
 TotalBarPrice =  from tr in db.Transactions
                  where tr.ReservationId = u.ReservationId
                  group tr by ......
                  g.sum(x =>x. ....)

0 个答案:

没有答案