【问题标题】:How to convert this SQL QUERY TO LINQ for Entity Framework如何将此 SQL QUERY 转换为 LINQ for Entity Framework
【发布时间】:2019-09-02 04:23:56
【问题描述】:

我是 LINQ 的新手,现在正在尝试学习查询的基础知识,由于 Entity Framework,我需要将此 SQL 转换为 LINQ。

SELECT Desktop, Notebook,Gmail, Telefone, Smartphone, Rede, Outros 
FROM Atributos, Gestors, Funcionario, CentroCusto 
WHERE Funcionario.Id = Atributos.Funcionario_Id 
AND Funcionario.CentroCusto_Id = CentroCusto.Id 
AND CentroCusto.Gestor_Id = Gestors.Id 
AND Gestors.Email = 'john@email.com'

【问题讨论】:

  • 到目前为止您尝试过什么?你遇到了什么问题?请展示你的尝试。

标签: c# sql asp.net entity-framework linq


【解决方案1】:

这里的另一个问题对此有非常详细的解释:How to do a join in linq to sql with method syntax?

但是,我认为这就是您要寻找的:

from g in Gestors
    join f in Funcionario on g.Id equals f.Gestor_Id 
    join a in Atributos on f.Id equals a.Funcionario_Id
    join c in CentroCusto on f.CentroCusto_Id equals c.Id
where g.Email == "john@email.com"
select new 
{
    //your fields with their tables prefixes
}

对于连接,我喜欢使用这种语法,因为对于刚开始使用 Linq 的人来说,它更易于阅读并且更易于从 SQL 转换。您可以使用 Lambda 语法,但如果您刚刚开始,这将是最容易掌握的,直到您对 Lambda 进行了更多练习。 另外,由于我不知道上述查询中的表结构是什么样的(索引、fk、pk),您可能可以显着提高性能。

【讨论】:

    【解决方案2】:

    实体创建映射到您的数据库的类。下面是获取数据的类和查询示例

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication108
    {
        class Program
        {
    
            static void Main(string[] args)
            {
                string email = "john@email.com";
                DataBase db = new DataBase();
                var results = (from atr in db.Atributos
                               join fun in db.Funcionario on atr.Funcionario_Id equals fun.Id
                               join cen in db.CentroCusto on fun.CentroCusto_Id equals cen.Id
                               join ges in db.Gestors on cen.Gestor_Id equals ges.Id
                               where ges.Email == email
                               select new { desktop = atr.Desktop, notebook = atr.Notebook, gmail = atr.Gmail, tele = atr.Telefone, smartPhone = atr.Smartphone, rede = atr.Rede, outros = atr.Outros })
                               .ToList();
            }
    
        }
        public class DataBase
        {
            public List<Atributos> Atributos { get; set; }
            public List<Gestors> Gestors { get; set; }
            public List<Funcionario> Funcionario { get; set; }
            public List<CentroCusto> CentroCusto { get; set; }
        }
        public class Atributos
        {
            public string Funcionario_Id { get; set; }
    
            public string Desktop { get; set; }
            public string Notebook { get; set; }
            public string Gmail { get; set; }
            public string Telefone { get; set; }
            public string Smartphone { get; set; }
            public string Rede { get; set; }
            public string Outros { get; set; }
         }
        public class Gestors
        {
            public string Id { get; set; }
            public string Email { get; set; }
        }
        public class Funcionario
        {
            public string Id { get; set; }
            public string CentroCusto_Id { get; set; }
        }
        public class CentroCusto
        {
            public string Id { get; set; }
            public string Gestor_Id { get; set; }
        }
    
    
    
    
    
    
    }
    

    【讨论】:

    • 为我工作!我认为 SQL InnerJoin 的概念可以帮助我更好地理解如何在 LINQ 中执行此查询,谢谢
    • 当我尝试构建视图时,我发现了这个异常:无法在 LINQ to Entities 查询中构建实体,您知道如何解决这个问题吗?
    猜你喜欢
    • 1970-01-01
    • 2021-05-09
    • 1970-01-01
    • 2015-10-02
    • 1970-01-01
    • 2015-12-12
    • 2015-09-11
    • 1970-01-01
    相关资源
    最近更新 更多