linq vs t SQL语法

时间:2017-01-05 12:17:04

标签: sql linq syntax

我是linq的新手,我以前用T-SQL开发。 我在T-SQL中有一个'简单'的查询,如下所示:

select article.Id, article.code,article.designation1,sum(inventaire.quantite),
sum(inventaire.quantite) * article.PrixStockArticle,
sum(inventaire.quantite) * article.PrixVenteArticle 
from inventaire 
inner join article on article.Id = inventaire.articleId 
left outer join articleFamille on articleFamille.Id = article.cleClientArticleFamille 
left outer join articleFamille2 on articleFamille2.Id = article.cleClientArticleFamille2 
group by article.Id,article.code, article.designation1,article.PrixVenteArticle,article.PrixStockArticle

我想用linq和实体框架进行此查询。 制作多个左外连接,分组依和和的简单方法是:

var result = (from f in _context.Inventaire.Include(i => i.oArticle)
join oArticle in _context.Article on f.articleId equals oArticle.Id
join oArticleFamille in _context.ArticleFamille on oArticle.cleClientArticleFamille equals oArticleFamille.Id
into InventaireList
from subpet in InventaireList.DefaultIfEmpty()
join oArticleFamille2 in _context.ArticleFamille2 on oArticle.cleClientArticleFamille2 equals oArticleFamille2.Id
into InventaireList2
from subpet2 in InventaireList2.DefaultIfEmpty()
where (f.clientId == oUser.clientId)
let essai = new
{
oArticle = f.oArticle
,
codeFamille = subpet == null ? "" : subpet.code
,
codeFamille2 = subpet2 == null ? "" : subpet2.code                         

}
orderby essai.oArticle.code
group f by essai into fGroup
select new InventaireViewModel
{
oArticle = fGroup.Key.oArticle
,
quantite = fGroup.Sum(f => f.quantite)
,
valStock = fGroup.Sum(f => f.quantite) * fGroup.Key.oArticle.PrixStockArticle
                       ,
valVente = fGroup.Sum(f => f.quantite) * fGroup.Key.oArticle.PrixVenteArticle
}).ToList();

有没有更简单的方法来进行这个linq查询? 两个解决方案,我对linq或linq太糟糕了,对于内连接和非常简单的查询都有好处,但不适用于真正的应用程序(如果我要在T-SQL中生成31行代码而不是4行,为什么MS建议使用linq ????? 为什么MS没有实现左外连接?他们从不使用左外?

感谢您的回复! 好的开发!

0 个答案:

没有答案
相关问题