繁体   English   中英

C#Linq to SQL-IEnumerable联接两个数据表/上下文

[英]C# Linq to SQL - IEnumerable Join Two Data Tables/Context

我正在将Visual Studio 2010,ASP.NET MVC 3与SQL 2008 R2数据库一起使用。

我相信这是一个简单的任务,但我不完全知道该如何措辞。 这是我当前的代码。

 public static IEnumerable GetAPCBundledCodesData(string CPTCode)
    {

        var result = (from item in new VADataContext().CPT2MODs
                      where item.CPT == CPTCode
                      select new { item.MOD }).ToList();
        return result;
    }

这将拉出CPTSMODs表中的项目列表并返回它。 我想做的是获取该结果,并获得另一个列表,该列表与每个具有相同值的项目匹配,该列表在另一个具有相同字段MOD的表中称为“修饰符”。 我不确定该如何做。

我希望我已经解释得足够好了。 让我知道是否需要任何澄清。

我一直在寻找答案,发现我的问题是我可以在on语句中包含两个DataContext。 我什至不知道如果不是两个DataContext,这是否行得通,但我尝试这样做:

public static IEnumerable GetAPCBundledCodesData(string CPTCode)
    {

        var result = (from mod in new VADataContext().MODIFIERs
                      join cpt2mod in new VADataContext().CPT2MODs on mod.MOD equals cpt2mod.MOD
                      where cpt2mod.CPT == CPTCode
                      select new { mod.MOD, mod.SHORT }).ToList();
        return result;
    }

这导致出现错误,提示我无法从两个不同的来源提取数据。 有其他处理方式吗?

这是一些示例数据,以显示此处发生的情况。

表CPT2MOD

CPT**   MOD**
31624   TC
31624   A
31624   DC
99213   B
99213   T
00100   AS

表修改器

MOD**   SHORT**
TC      TC Desc
A       A Desc
DC      DC Desc
B       B Desc
T       T Desc
AS      AS Desc

CPT代码将传递到GetAPCBundledCodesData(ex 99213),它将从CPT2MOD表(B,T)中查找与其关联的所有MOD值。 然后它将返回MODIFIERs表中的那些MOD及其说明。 (B,B Desc; T,T Desc)。

希望您现在能明白我的要求。

public static IEnumerable GetAPCBundledCodesData(string CPTCode)
        {
            using (var dc = new VADataContext()){
            var result = (from a in dc.CPT2MODs
                          where a.CPT == CPTCode
                          join b in dc.MODIFIERs on a.MOD equals b.MOD
                          select new { b.MOD, b.SHORT }).ToList();
            return result;
            }
        }

仅使用一个ObjectContext-并使用using (非常重要):

using(var dc = new VADataContext()){
  var result = (from mod in dc.MODIFIERs 
                  join cpt2mod in dc.CPT2MODs on mod.MOD equals cpt2mod.MOD 
                  where cpt2mod.CPT == CPTCode 
                  select new { mod.MOD, mod.SHORT }).ToList(); 
  return result; 
}

DataContext说一句-实体框架通常不调用它生成DataContext的类-这是Linq to SQL的东西-您确定您实际上在使用实体框架吗? 它实际上与修复没有任何关系,只是一个澄清的要求。

暂无
暂无

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

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