日期:2014-05-20  浏览次数:20844 次

sql to linq
select d.DishShopID, d.DishName,d.DishPrice,d.DishImageUrl,AVG(c.CommentScore) as Comments from Dishes d left join Comments c on d.DishID=c.CommentDishID where d.DishShopID=5 group by d.DishName,d.DishPrice,d.DishImageUrl,d.DishShopID order by Comments desc

外联如何表示?left join.

------解决方案--------------------
参考:
Linq实现Left join
http://developer.51cto.com/art/200909/152189.htm
------解决方案--------------------
http://msdn.microsoft.com/zh-cn/library/bb397895.aspx
------解决方案--------------------
C# code

var query=from d in db.Dishes
          join c in db.Comments
          on d.DishID equals c.CommentDishID into lf
          from c in lf.DefaultIfEmpty()
          where d.DishShopID==5
          group d by new {d.DishName,d.DishPrice,d.DishImageUrl,d.DishShopID} into g
          let Cts=g.Average(x=>x.CommentScore)
          orderby Cts descending
          select new 
          {
             g.Key.DishShopID,
             g.Key.DishName,
             g.Key.DishPrice,
             g.Key.DishImageUrl,
             Comment=Cts
          };

------解决方案--------------------
探讨

C# code

var query=from d in db.Dishes
join c in db.Comments
on d.DishID equals c.CommentDishID into lf
from c in lf.DefaultIfEmpty()
where d.DishShopID==5
……

------解决方案--------------------
探讨

select d.DishShopID, d.DishName,d.DishPrice,d.DishImageUrl,AVG(c.CommentScore) as Comments from Dishes d left join Comments c on d.DishID=c.CommentDishID where d.DishShopID=5 group by d.DishName,d.Di……