如何使用LINQ扩展方法创建多个连接?(How do I create multiple joins using LINQ extension methods?)

我在使用具有多个连接的LINQ方法调用时遇到问题。 我正在尝试做这样的事情:

if (!isDepSelect) { query = (from Items in db.DEPARTMENTs select Items); } else { query = (from Items in db.DEPARTMENTs from gDept in db.DEPT_PROFILE from wAccess in db.WEB_ACCESS where Items.DEPT_CODE == gDept.DEPT_CODE && gDept.USER_ID == wAccess.USER_ID && wAccess.EMP_ID == id select Items); }

我这样做了:

IQueryable<DEPARTMENT> query = db.DEPARTMENTs; if (isDepSelect) { query = query.Join(db.DEPT_PROFILE,depts => depts.DEPT_CODE,prof => prof.DEPT_CODE,(depts, prof) => depts); }

但现在我不知道如何使用WEB_ACCESS表和EMP_ID = id的条件添加DEPT_PROFILE表的JOIN。

我这样做的原因是isDepSelect布尔值不是此查询将改变其关系的唯一条件,我需要在不重复我的每个条件的LINQ的情况下添加此关系。

感谢您的时间。

I'm having trouble using LINQ method calls with multiple joins. I'm trying to do something like this:

if (!isDepSelect) { query = (from Items in db.DEPARTMENTs select Items); } else { query = (from Items in db.DEPARTMENTs from gDept in db.DEPT_PROFILE from wAccess in db.WEB_ACCESS where Items.DEPT_CODE == gDept.DEPT_CODE && gDept.USER_ID == wAccess.USER_ID && wAccess.EMP_ID == id select Items); }

I had done this:

IQueryable<DEPARTMENT> query = db.DEPARTMENTs; if (isDepSelect) { query = query.Join(db.DEPT_PROFILE,depts => depts.DEPT_CODE,prof => prof.DEPT_CODE,(depts, prof) => depts); }

But now I don't know how to add the JOIN of DEPT_PROFILE table with the WEB_ACCESS table and the condition of the EMP_ID = id.

The reason I'm doing this is that the isDepSelect boolean is not the only condition that this query will change its relations and I need someway to add this relations without repeating my LINQ for each of my conditions.

Thank you for your time.

最满意答案

试试吧,

List<DEPARTMENTs> list = db.DEPARTMENTs.Join(db.DEPT_PROFILE, dept => dept.DEPT_CODE, prof => prof.DEPT_CODE, (dept,prof) => new {dept, prof}) .Join(Wdb.WEB_ACCESS, depts => depts.prof.USER_ID,web => web.USER_ID,(depts,web) => new { depts, web}) .Where(result => result.web.EMP_ID== id).Select(s => s.depts.dept).ToList<DEPARTMENTs>();

Try with,

List<DEPARTMENTs> list = db.DEPARTMENTs.Join(db.DEPT_PROFILE, dept => dept.DEPT_CODE, prof => prof.DEPT_CODE, (dept,prof) => new {dept, prof}) .Join(Wdb.WEB_ACCESS, depts => depts.prof.USER_ID,web => web.USER_ID,(depts,web) => new { depts, web}) .Where(result => result.web.EMP_ID== id).Select(s => s.depts.dept).ToList<DEPARTMENTs>();如何使用LINQ扩展方法创建多个连接?(How do I create multiple joins using LINQ extension methods?)

我在使用具有多个连接的LINQ方法调用时遇到问题。 我正在尝试做这样的事情:

if (!isDepSelect) { query = (from Items in db.DEPARTMENTs select Items); } else { query = (from Items in db.DEPARTMENTs from gDept in db.DEPT_PROFILE from wAccess in db.WEB_ACCESS where Items.DEPT_CODE == gDept.DEPT_CODE && gDept.USER_ID == wAccess.USER_ID && wAccess.EMP_ID == id select Items); }

我这样做了:

IQueryable<DEPARTMENT> query = db.DEPARTMENTs; if (isDepSelect) { query = query.Join(db.DEPT_PROFILE,depts => depts.DEPT_CODE,prof => prof.DEPT_CODE,(depts, prof) => depts); }

但现在我不知道如何使用WEB_ACCESS表和EMP_ID = id的条件添加DEPT_PROFILE表的JOIN。

我这样做的原因是isDepSelect布尔值不是此查询将改变其关系的唯一条件,我需要在不重复我的每个条件的LINQ的情况下添加此关系。

感谢您的时间。

I'm having trouble using LINQ method calls with multiple joins. I'm trying to do something like this:

if (!isDepSelect) { query = (from Items in db.DEPARTMENTs select Items); } else { query = (from Items in db.DEPARTMENTs from gDept in db.DEPT_PROFILE from wAccess in db.WEB_ACCESS where Items.DEPT_CODE == gDept.DEPT_CODE && gDept.USER_ID == wAccess.USER_ID && wAccess.EMP_ID == id select Items); }

I had done this:

IQueryable<DEPARTMENT> query = db.DEPARTMENTs; if (isDepSelect) { query = query.Join(db.DEPT_PROFILE,depts => depts.DEPT_CODE,prof => prof.DEPT_CODE,(depts, prof) => depts); }

But now I don't know how to add the JOIN of DEPT_PROFILE table with the WEB_ACCESS table and the condition of the EMP_ID = id.

The reason I'm doing this is that the isDepSelect boolean is not the only condition that this query will change its relations and I need someway to add this relations without repeating my LINQ for each of my conditions.

Thank you for your time.

最满意答案

试试吧,

List<DEPARTMENTs> list = db.DEPARTMENTs.Join(db.DEPT_PROFILE, dept => dept.DEPT_CODE, prof => prof.DEPT_CODE, (dept,prof) => new {dept, prof}) .Join(Wdb.WEB_ACCESS, depts => depts.prof.USER_ID,web => web.USER_ID,(depts,web) => new { depts, web}) .Where(result => result.web.EMP_ID== id).Select(s => s.depts.dept).ToList<DEPARTMENTs>();

Try with,

List<DEPARTMENTs> list = db.DEPARTMENTs.Join(db.DEPT_PROFILE, dept => dept.DEPT_CODE, prof => prof.DEPT_CODE, (dept,prof) => new {dept, prof}) .Join(Wdb.WEB_ACCESS, depts => depts.prof.USER_ID,web => web.USER_ID,(depts,web) => new { depts, web}) .Where(result => result.web.EMP_ID== id).Select(s => s.depts.dept).ToList<DEPARTMENTs>();