实体框架 - 我怎样才能实现这个SQL的简称EF LINQ [英] entity framework - how can I implement this SQL in the abbreviated EF linq

查看:167
本文介绍了实体框架 - 我怎样才能实现这个SQL的简称EF LINQ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能帮帮忙什么的C#code是执行的简写本SQL作为实体框架LINQ的? (例如,你有。符号,如xxx.where(...等)

  SELECT PN.Name,SUM(U.Amount)
从用法为U,ProcessNames为PN
WHERE PN.Id == U.ProcessNameId
   与U.Datetime之间的'2010-01-08'和'2010-10-11'
集团通过PN.Name
 

解决方案

方法为基础的查询:
要实现这一点的lambda我们需要利用 Queryable.GroupJoin

  VAR的查询= context.ProcessNames
    .GroupJoin(context.Usages
                      。凡(U => u.Datetime> =新的日期时间(2010年,1,8))
                                  && u.Datetime< =新的日期时间(2010,10,11),
               PN => pn.Id,
               U => u.ProcessNameId,
               (PN,用法)=>新{名称= pn.Name,
                                     总和= usages.Sum(U => u.Amount)});
 


查询前pression:
而在查询前pression语法非常相同的查询:

  VAR的查询=
    从PN在context.ProcessNames
    加入ü在context.Usages
                     。凡(U => u.Datetime> =新的日期时间(2010年,1,8))
                                 && u.Datetime< =新的日期时间(2010,10,11),
    在pn.Id
    等于u.ProcessNameId
    为G
    选择新{名称= pn.Name,
                 总和= g.Sum(U => u.Amount)};
 


查看生成的SQL:
为了验证这些查询给你你想要的SQL命令,在运行时,你可以这样做:

 字符串的SqlCommand =((的ObjectQuery)查询).ToTraceString();
 


更多的例子:
有关群组加入一些很好的例子,请大家一起来看看这些:

方法为基础的查询语法示例:加入运营商
查询前pression语法示例:加入运营商

Can anyone help out with what the C# code would be to implement this SQL as Entity Framework Linq in the abbreviated form? (e.g. where you have the "." notation such as xxx.where(... etc)

SELECT PN.Name, Sum(U.Amount)
FROM Usages as U, ProcessNames as PN
WHERE PN.Id == U.ProcessNameId 
   AND U.Datetime BETWEEN '2010-01-08' AND '2010-10-11'
Group By PN.Name

解决方案

Method-Based Query:
To implement this in lambda we need to leverage Queryable.GroupJoin:

var query = context.ProcessNames
    .GroupJoin(context.Usages
                      .Where(u => u.Datetime >= new DateTime(2010, 1, 8) ) 
                                  && u.Datetime <= new DateTime(2010, 10, 11),
               pn  => pn.Id,
               u => u.ProcessNameId, 
               (pn, usages) => new { Name = pn.Name, 
                                     Sum = usages.Sum(u => u.Amount) });


Query Expression:
And the very same query in query expression syntax:

var query = 
    from pn in context.ProcessNames
    join u in context.Usages
                     .Where(u => u.Datetime >= new DateTime(2010, 1, 8) ) 
                                 && u.Datetime <= new DateTime(2010, 10, 11),
    on pn.Id 
    equals u.ProcessNameId 
    into g                      
    select new { Name = pn.Name, 
                 Sum = g.Sum(u => u.Amount) };


Check the Generated SQL:
To verify that these queries give you your desired Sql command at runtime you can do this:

string sqlCommand = ((ObjectQuery)query).ToTraceString();


More Examples:
For some good examples on GroupJoin, please take a look at these:

Method-Based Query Syntax Examples: Join Operators
Query Expression Syntax Examples: Join Operators

这篇关于实体框架 - 我怎样才能实现这个SQL的简称EF LINQ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆