2014-03-01 119 views
1

你好我有一個C#lambda表達式看起來應該對我工作,但它沒有返回任何東西。需要幫助修復lambda連接C#

   CategoryItems = (db.Items.Include("Pictures").Join(db.Rentals, 
                   i => i.itemID, 
                   r => r.ItemID, 
                   (i, r) => new { Item = i, Rental = r })               
                   .Where(ir => ir.Item.CategoryID == CategoryID && ir.Rental.RentedBy == 0) 
                   .OrderByDescending(ir => ir.Item.ListDate) 
      .Select(i => new DisplayItem() 
      { 
       AvailableForPurchase = i.Item.AvailableForPurchase, 
       Description = i.Item.Description == string.Empty ? "No Description" : i.Rental.Title, 
       PostDate = i.Item.ListDate, 
       PostedBy = i.Item.User.UserName, 
       PricePerDay = i.Rental.RentalPrice ?? 0.00m, 
       ItemID = i.Item.itemID, 
       PhotoURL = i.Item.Pictures.FirstOrDefault().PictureLink 
      })).ToPagedList(page, 5); 

任何幫助表示讚賞

+0

您是否正在使用lambda語法設置?我發現加入查詢語法更容易。 – clhereistian

回答

0
var CategoryItems = 
from item in db.Items 
join rental in db.Rentals on item.itemID equals rental.ItemID 
where item.CategoryID == CategoryID && rental.RentedBy == 0 
orderby item.ListDate descending 
select new DisplayItem 
{ 
    AvailableForPurchase = item.AvailableForPurchase, 
    ... 
}; 
+0

原來我的lambda很好。但是,默認情況下,rentedby爲null,而不是0.將標記爲正確,因爲這是查詢語法equiv。謝謝=) – Mike