LINQ to SQL的 - 填充连接结果到一个列表 [英] Linq to Sql - Populate JOIN result into a List

查看:303
本文介绍了LINQ to SQL的 - 填充连接结果到一个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道,如果可以做到这一点,但这里的情景。

我想打开这个SQL到LINQ:

  SELECT * FROM部ð
INNER JOIN职工E在e.DepartmentID = d.DepartmentID
 

部门 - 员工是1对多的关系

我创建了一个自定义对象,我想填充的结果之中。

 公共类DepartmentSummary
{
    公共部门部门{获得;组; }
    公开名单<员工>员工{获得;组;}
}
 

我想出了LINQ的是

  VAR的结果=从D在dba.Department
             加入E在dba.Employee d.DepartmentID等于e.DepartmentID到J1
             选择新DepartmentSummary
             {
                  部= d的,
                  员工= j1.ToList()
             };
 

我尝试过了,它不工作。任何人都可以提供一些线索给我好吗?我想执行部门和员工之间的内部连接。每个部门在结果,我想创建持有该部门以及属于该部门的员工的列表中的一个DepartmentSummary对象。

是否的LINQ提供了一个临时解决方案,这还是我必须遍历结果集,并创建DepartmentSummary列表中手动?

谢谢

编辑: 看起来这对我的作品

  VAR的结果=从D在dba.Department
             加入E在dba.Employee d.DepartmentID等于e.DepartmentID到J1
             其中j1.Count()> 0
             选择新DepartmentSummary
             {
                  部= d的,
                  员工= j1.ToList()
             };
 

解决方案

听起来你正在寻找解决延迟加载?

  DataLoadOptions DLO =新DataLoadOptions();
dlo.LoadWith<部>(D => d.Employees);
使用(VAR DBA =新MyDataContext())
{
  dba.LoadOptions = DLO;
  VAR的结果=从D在dba.Department
      选择D组;
}
 

现在,如果你没有部门和员工之间形成的关系(该LINQ2SQL设计师会,如果你有数据库关系设置为你这样做),那么你应该考虑这样做。它使这一切要方便得多。事实上,你甚至不需要你的活动总结。

I am not sure if this can be done, but here's the scenario.

I want to turn this sql into linq:

SELECT * FROM Department d
INNER JOIN Employee e ON e.DepartmentID = d.DepartmentID

Department - Employee is 1 to many relationship.

I have created a custom object that I would like to populate the result into.

public class DepartmentSummary
{
    public Department Department { get; set; }
    public List<Employee> Employees {get; set;}
}

The Linq I came up with is

var result = from d in dba.Department
             join e in dba.Employee d.DepartmentID equals e.DepartmentID into j1
             select new DepartmentSummary
             {
                  Department = d,
                  Employees = j1.ToList()
             };

I tried it out and it's not working. Can anyone shed some light for me please? I would like to perform an inner join between Department and Employee. For each Department in the resultset, I would like to create one DepartmentSummary object which holds that department and a list of employees belonging to that department.

Does Linq provides an ad hoc solution for this or must I iterates through the result set and create a list of DepartmentSummary manually?

Thanks,

EDIT: Looks like this works for me

var result = from d in dba.Department
             join e in dba.Employee d.DepartmentID equals e.DepartmentID into j1
             where j1.Count() > 0
             select new DepartmentSummary
             {
                  Department = d,
                  Employees = j1.ToList()
             };

解决方案

Sounds like you're looking to get around lazy loading?

DataLoadOptions dlo = new DataLoadOptions();
dlo.LoadWith<Department>(d => d.Employees);
using (var dba = new MyDataContext())
{
  dba.LoadOptions = dlo;
  var result = from d in dba.Department
      select d;
}

Now, if you don't have a relationship defined between Department and Employees (the Linq2Sql designer will do this for you if you have database relationships setup) then you should look into doing that. It makes it all dramatically easier. In fact, you don't even need your campaign summary.

这篇关于LINQ to SQL的 - 填充连接结果到一个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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