减少Linq Joins的冗长程度?

时间:2016-02-22 20:08:54

标签: c# entity-framework linq

我目前正在努力弄清楚如何用Entity Framework和Linq替换所有跨数据库连接,我已经设法让我的代码的一部分工作,但真正使它成为一个令人讨厌的是连接的冗长和复杂性。是否有任何方法可以简化代码,或者我是否遇到冗长,冗长,凌乱的代码?

一个例子:

using (var context = new CustomerContext(CustomerID))
using (var e = new eContext())
{
    var globalUserList = e.GlobalLoginCustomerBridges
        .Join(e.GlobalLogins,
            glcb => glcb.glcbr_gl_id,
            gl => gl.gl_id,
            (glcb, gl) => new { glcb, gl })
        .Where(n => n.glcb.glcbr_customer_id == CustomerID)
        .Select(n => new User2
        {
            ID = (int)n.glcb.glcbr_user_id,
            GlobalLogin = n.gl.gl_login_name,
            GUID = n.gl.gl_GUID
        }).ToList();

    var customer = e.Customers
        .Join(e.DatabaseConnectionStrings,
        c => c.DatabaseConnectionID,
        d => d.DatabaseConnectionID,
        (c, d) => new { c, d })
        .Select(n => new Customer2
        {
            ID = n.c.CustomerID,
            Name = n.c.CustomerName,
            DatabaseConnectionName = n.d.DatabaseConnectionName,
            DatabaseConnectionString = n.d.DatabaseConnectionString1,
            GUID = n.c.cust_guid,
        }).ToList().FirstOrDefault(n => n.ID == CustomerID);

    var orgs = context.Organizations
        .Select(o => new Organization2
        {
            ID = o.org_id,
            Name = o.org_name,
        }).ToList();

    var users = context.Users
        .Select(n => new User2
        {
            ID = n.UserID,
            FirstName = n.UserFirstName,
        }).ToList();

    var userList = users
        .Join(globalUserList,
            u => u.ID,
            gl => gl.ID,
            (u, gl) => new { u, gl })
        .Join(context.OrganizationObjectBridges,
            u => u.u.ID,
            oob => oob.oob_object_id,
            (u, oob) => new { u, oob })
            .Where(o => o.oob.oob_object_type_id == 9)
        .Select(n => new User2
        {
            ID = n.u.u.ID,
            GlobalLogin = n.u.gl.GlobalLogin,
            FirstName = n.u.u.FirstName,
            GUID = n.u.gl.GUID,
            Customer = customer,
            Organization = orgs.FirstOrDefault(o => o.ID == n.oob.oob_org_id)
        }).Where(n => !isDisabled != null && n.Disabled == isDisabled).ToList();

    return userList;
}

在上面的代码段中,我删除了约80%的代码,因为大多数代码只是字段映射,但它比显示的内容长得多。

1 个答案:

答案 0 :(得分:2)

似乎前两个查询不需要加入,因为它们属于同一个数据库。你不能使用导航属性吗?请记住,只使用没有物理(导航属性)关系的对象之间的连接。

关于最后一个查询,您可以使用linq查询而不是链式方法(在我看来,这使得代码更具可读性)。它会是这样的:

var userList = from user in users
                join gul in globalUserList on user.ID = gul.ID
                join oob in context.OrganizationObjectBridges on user.ID = oob.oob_object.id
                where oob.oob_object_type_id == 9
                select new User2
                {
                    ID = user.ID,
                    GlobalLogin = gul.GlobalLogin,
                    FirstName = user.FirstName,
                    GUID = gul.GUID,
                    Customer = customer,
                    Organization = orgs.FirstOrDefault(o => o.ID == n.oob.oob_org_id)
                };

未经测试的代码,我相信它不起作用。我只是给你一些想法。