在使用LINQ和Entity Framework笛卡尔积结合3个表 [英] Using a cartesian product in LINQ and Entity framework to combine 3 tables

查看:251
本文介绍了在使用LINQ和Entity Framework笛卡尔积结合3个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

才返回set.select我想包括从另一个表中的字段,但我canot加入这个表,因为它没有共同领域与其他两个表。我怎么可能会调整我的code以下,以实现这一目标? IAM使用vs2012 SQL和MVC C#

  VAR套=
    (从管理者米
     从context.tblCompanyŤ
     加入TSC在context.tblStyling上t.ccID等于tsc.ccID
     选择新的{tsc.ccID,LogoIcon = tsc.Icon,tsc.style1,tsc.style2,t.Desc})
    .ToList();返回set.Select(C =>新建设置(c.ccID,c.style1,c.style2,c.Desc,c.LogoIcon,m.firstName,m.lastName));


解决方案

好吧,它看起来像你想加入 context.tblCompany 背景.tblStyle 并获得结果集管理​​交叉产品和。如果这是正确的,那么你就已经存在。你只需要包括管理​​要在你的SELECT语句中的字段:

  VAR套=
    (从管理者米
     从context.tblCompanyŤ
     加入TSC在context.tblStyling上t.ccID等于tsc.ccID
     新选择
        {
            tsc.ccID,
            LogoIcon = tsc.Icon,
            tsc.style1,
            tsc.style2,
            t.Desc,
            m.firstName,
            m.lastName})
    .ToList();返回设置;

before I return "set.select" I would like to include fields from another table but I canot join this table because it has no fields in common with the other two tables. How may I adjust my code below to achieve this? Iam using vs2012 sql and in MVC c#

var set = 
    (from m in managers
     from t in context.tblCompany
     join tsc in context.tblStyling on t.ccID equals tsc.ccID
     select new { tsc.ccID,LogoIcon = tsc.Icon , tsc.style1, tsc.style2, t.Desc })
    .ToList();

return set.Select(c => new Settings(c.ccID, c.style1, c.style2, c.Desc, c.LogoIcon , m.firstName , m.lastName));

解决方案

Okay, it looks like you want to join context.tblCompany and context.tblStyle and get the cross product of the resulting set and managers. If that is correct, then you are already there. You just need to include the fields from manager that you want in your select statement:

var set = 
    (from m in managers
     from t in context.tblCompany 
     join tsc in context.tblStyling on t.ccID equals tsc.ccID
     select new 
        { 
            tsc.ccID, 
            LogoIcon = tsc.Icon, 
            tsc.style1, 
            tsc.style2, 
            t.Desc, 
            m.firstName, 
            m.lastName })
    .ToList();

return set;

这篇关于在使用LINQ和Entity Framework笛卡尔积结合3个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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