简体   繁体   English

如何在 LINQ C# 中有效地选择导航属性

[英]How to Select a Navigation Property efficiently in LINQ C#

I'm having Three Table我有三桌

Table #1 : BossEmp表 #1:BossEmp

SNo     JobID     BossID      EMPID       StartDt
_____________________________________________________
  1        1           6          1       05-20-2016
  2        1           6          2       05-20-2016
  3        2           7          3       06-20-2016
  4        2           7          4       06-20-2016
  5        2           7          5       06-20-2016

Table #2 : Emplyee表 #2:员工

EmpID    EmpName      Gender    DOB           Dep
_________________________________________________________    
    1    Sakthivel     M        12-11-1986    Development
    2    Regina        F        04-03-1989    Development
    3    Samantha      F        12-12-1987    Development
    4    Keerthi       F        08-18-1988    Development
    5    Pranitha      F        11-10-1985    Development
    6    Vijay         M        02-21-1987    Development
    7    Bhavana       F        12-06-1985    Development

Table # 3 : Job表#3:工作

JobID     Title            Description
__________________________________________
    1     RSI              Description RSI
    2     MSI              Description MSI

In Table #1 BossEMP -> JobID is a Foreign Key from the Job Table and BossEmp -> BossID, EmpID are a Foreign Key from the Employee Table .在表#1 BossEMP -> JobID是来自Job Table外键BossEmp -> BossID, EmpID是来自Employee Table外键

The EDMX Class Diagram is EDMX 类图是

在此处输入图片说明

BossEmps -> EmpID === Employee -> EmpID
BossEmps1 -> BossID === Employee -> EmpID

BossEmps -> JobID === Job -> JobID

Now I Need to Create an Object of a Model现在我需要创建一个模型的对象

Class WorkInfo
{
    public List<Employee> EmpList { get; set; }
    public Job JobInfo { get; set; }
}

Now I need to Create List<WorkInfo> , it should contain only Female.现在我需要创建List<WorkInfo> ,它应该只包含女性。

Kindly assist me how to select Navigation Property efficiently in LINQ C# to construct List<WorkInfo>请帮助我如何在 LINQ C# 中有效地选择导航属性来构造List<WorkInfo>

The Database contains more than 1000K Records.数据库包含超过 1000K 的记录。

I tried the following Code:我尝试了以下代码:

using (var db = new EmployeeEntities()) {
                db.BossEmps.Where(b => b.Employee.Gender == "F").Select(e => new {
                    Emp = new {
                        Name = e.Employee.EmpName,
                        Id = e.Employee.EmpId
                    },
                    JobInfo = new {
                        Name = e.Job.Title,
                        Id = e.Job.JobID
                    }
                }).GroupBy(x => x.JobInfo).ToList();
            }

If I got this right you need all the female employees who has same job.如果我做对了,您需要所有从事相同工作的女性员工。 In this case I think you are looking for somethig like this:在这种情况下,我认为您正在寻找这样的东西:

var workInfo = context.BossEmp.Select(b => new
        {
            EmpList = b.Employes.Where(e => b.EmployeId == e.EmployeId && e.Gendar.Equals("F")),
            Job = b.Jobs.FirstOrDefault(j => b.JobId == j.JobId)
        });

This will create collection of objects with properties EmpList and Job.这将创建具有 EmpList 和 Job 属性的对象集合。 If you need it as List you can use ToList() method.如果你需要它作为 List 你可以使用 ToList() 方法。

var employees = workInfo.EmpList.ToList();

and/or和/或

var workInfos = workInfo.toList();

Here is a sample I quickly put together for you based on your classes.这是我根据您的课程快速为您整理的示例。

This should select all females, with their corresponding Job info and employee info, from the BossEmp collection.这应该从 BossEmp 集合中选择所有女性,以及她们相应的工作信息和员工信息。

public class Program
{
    public static void Main(string[] args)
    {
        Program p = new Program();
    }

    public Program()
    {
        var bossEmpCollection = new List<BossEmp>()
        {
            new BossEmp() { SNo = 2, JobID = 1, BossID = 6, EmpID = 2, StartDt = DateTime.Now },
            new BossEmp() { SNo = 1, JobID = 1, BossID = 6, EmpID =  1, StartDt = DateTime.Now }
        };
        var employeeCollection = new List<Employee>()
        {
            new Employee() { EmpID = 1, EmpName = "Sakthivel", Gender = 'M', DOB = DateTime.Now, Dep = "Development" },
            new Employee() { EmpID = 2, EmpName = "Regina", Gender = 'F', DOB = DateTime.Now, Dep = "Development" }
        };
        var jobCollection = new List<Job>()
        {
            new Job() { JobID = 1, Title = "RSI", Description = "RSI" }
        };

        var workInfoCollection = from bEmp in bossEmpCollection
                                 join e in employeeCollection on bEmp.EmpID equals e.EmpID
                                 join j in jobCollection on bEmp.JobID equals j.JobID
                                 where e.Gender.Equals('F')
                                 select new WorkInfo() { EmpObject = e, JobInfo = j };

        foreach (var workInfo in workInfoCollection)
        {
            Console.WriteLine($"Emp Name: {workInfo.EmpObject.EmpName} Work Desc: {workInfo.JobInfo.Description}");
        }

        Console.ReadKey();
    }
}
public class BossEmp
{
    public int SNo { get; set; }
    public int JobID { get; set; }
    public int BossID { get; set; }
    public int EmpID { get; set; }
    public DateTime StartDt { get; set; }
}
public class WorkInfo
{
    public Employee EmpObject { get; set; }
    public Job JobInfo { get; set; }
}

public class Employee
{
    public int EmpID { get; set; }
    public string EmpName { get; set; }
    public char Gender { get; set; }
    public DateTime DOB { get; set; }
    public string Dep { get; set; }

}
public class Job
{
    public int JobID { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
}

Using this example, will construct the LINQ into one SQL SELECT statement, and only execute, once you loop through the workInfoCollection collection, making, one trip to the DB and back.使用此示例,将 LINQ 构造为一个 SQL SELECT 语句,并且仅在您循环遍历workInfoCollection集合时才执行,从而使一次到 DB 并返回。

Every single time you you access a Navigational Property on a model class, the deferred execution in the background, makes a connection to the DB, and only runs the query for that specific items you are wanting to see, essentially, making more calls to the Database, making this less efficient, in my opinion.每次您访问模型类上的导航属性时,后台的延迟执行都会连接到数据库,并且只运行您想要查看的特定项目的查询,本质上是对数据库,在我看来,这降低了效率。

If you run a SQL profiler, you will be able to verify this yourself.如果您运行 SQL 探查器,您将能够自己验证这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM