【问题标题】:Converting T-SQL to Linq将 T-SQL 转换为 Linq
【发布时间】:2011-06-27 21:21:57
【问题描述】:

我正在使用 Entitry Framework 4.1,我正在努力理解如何将以下使用连接和聚合方法的查询转换为 DomainService 中的 Linq to Entities 调用。

SELECT     tblTime.Period As Timeline, COUNT(tblEngineeringDashboard_ItemList.ID) AS Items
FROM         tblEngineeringDashboard_ItemList INNER JOIN
                      tblTime ON tblEngineeringDashboard_ItemList.TimeID = tblTime.ID
GROUP BY tblTime.Period
ORDER BY tblTime.Period

谁能提供帮助。

可能的解决方案

Dim var = From i In ObjectContext.tblEngineeringDashboard_ItemList
                Join t In ObjectContext.tblTimes On i.TimeID Equals t.ID
                Group By i.TimeID Into Group
                Select DateStart = (From n In ObjectContext.tblTimes Where n.ID = TimeID Select n.Period), PartCount = Group.Count

菲尔

【问题讨论】:

    标签: tsql entity-framework-4 linq-to-entities


    【解决方案1】:

    首先想到的是:

    var q = from t in Context.Time
            group t by t.Period into g
            orderby g.Key
            select new 
            {
                Timeline = g.Key,
                Items = (from ti in g
                         from il in ti.ItemList // or whatever the property for the navigation to tblEngineeringDashboard_ItemList is called
                         select il).Count()
            };
    

    但是,原始 SQL 有一个 INNER JOIN,它将拒绝 tblTime 记录,而 tblEngineeringDashboard_ItemList 中没有任何匹配记录。所以你可能想要:

    var q = from t in Context.Time
            where t.ItemList.Any()
            group t by t.Period into g
            orderby g.Key
            select new 
            {
                Timeline = g.Key,
                Items = (from ti in g
                         from il in ti.ItemList // or whatever the property for the navigation to tblEngineeringDashboard_ItemList is called
                         select il).Count()
            };
    

    您还可以翻转查询:

    var q = from i in Context.EngineeringDashboardItemList
            where i.Time != null 
            group i by i.Time.Period into g
            orderby g.Key
            select new 
            {
                Timeline = g.Key,
                Items = g.Count()
            };
    

    【讨论】:

    • 嗨克雷格,我接受了你的解决方案,在改变了一些事情之后让它工作但我不确定我明白为什么或者我的解决方案是否是最好的..... Dim var = From i In ObjectContext.tblEngineeringDashboard_ItemList Join t In ObjectContext.tblTimes On i.TimeID Equals t.ID Group By i.TimeID Into Group Select DateStart = (From n In ObjectContext.tblTimes Where n.ID = TimeID Select n.Period), PartCount = Group .计数
    • It is usually incorrect 在 LINQ to Entities 或 LINQ to SQL 中使用 Join()
    • 明白了,但是在创建笛卡尔积的情况下使用交叉连接时需要小心。它是跨越所有可能性的坚定不移的加入。不过 LINQ 更容易阅读。
    • 在使用 LINQ to Entities 导航时不存在意外创建笛卡尔积的危险。事实上,它们的优点之一是不可能搞砸JOIN 条件(“ON”)。
    【解决方案2】:

    这行得通吗?

    tblEngineeringDashboard_ItemList
        .Join(tblTime,ed => ed.TimeID ,t => t.ID, (ed,t) => new{ed,t})
    .GoupBy(g => g.t.Period)
    .Select(s => new 
              {
               Timeline = s.Key,
               Items = s.Count()
              }
           )
    .OrderBy(o => o.Timeline)
    

    【讨论】:

    • 完全不是。例如,该计数将给出在某个键下分组的 tblTime 行数,而不是在按键分组的所有项目下的外键表的总行数。检查我的答案。
    • 我可能弄错了,但考虑到它是一个内连接,它不应该返回正确的行数吗?
    • 在他的 sql 查询中,他正在计算连接提供的元素。这转化为 linq 中的 selectmany。如果他正在计算查询返回的行数,那么您的方法是正确的。
    【解决方案3】:

    虽然从 sql 转换为 linq 不是一种理想的方法(您应该直接在 linq 中思考,将您的需求转换为 linq 查询),但您发布的查询相当简单。

    var grouped = tblTime.OrderBy(c => c.Period).GroupBy(c => c.Period).Select(c =>
    new {
           timeline = c.Key,
           count = c.SelectMany(x => x.tblEngineeringDashboard).Count()
        });
    

    *编辑:那里,已修复。 L2E 引擎上的一切。

    前提是表之间有正确的外键(因此您不必手动声明连接)。

    【讨论】:

    • 这会给你 n+1 个查询,假设延迟加载已打开。如果延迟加载关闭,Count() 会出错。
    • 是的,我假设延迟加载已打开,因为我一直在使用它。我应该指定的。
    • n+1 仍然是个问题。你会得到正确的答案......最终。
    • 我没有看到 n+1 的问题。这种查询我做了很多次都没有问题。
    • 在 SQL 分析器中尝试一下。你正在懒洋洋地加载c.tblEngineeringDashboard。更糟糕的是,您正在加载单个记录,而不仅仅是计数。枚举grouped 将结果带入对象空间;你已经离开 L2E 并且 Count() 不能再被推回 SQL COUNT
    猜你喜欢
    • 2012-11-27
    • 2010-12-02
    • 1970-01-01
    • 1970-01-01
    • 2017-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多