实体 - 两个连接表的类型

时间:2017-06-07 13:08:26

标签: c# entity-framework linq

我需要从两个连接表中提取数据。数据集是什么类型的?我是否需要创建一个包含这两个表中属性的类以用于数据集类型,或者我可以使用实体模式中的类型。如何修改我的方法才能正常工作?

public static IEnumerable<Result> GetCarrier()
{
    IEnumerable <Result> result = new IEnumerable<>();
    try
    {
        using (MyEntities myEntity = new MyEntities())
        {
            var result = from s in Carriers car
                     CoreEnt ent ON car.CoreEntID equals ent.CoreEntID
                     where car.NeedsTransfer == True
                     select s;
        }
        return result;
    }
    catch (Exception ex)
    {
        return result;
    }
}

1 个答案:

答案 0 :(得分:4)

linq查询可以如下。

try
    {

        using (MyEntities myEntity = new MyEntities())
        {
            var result = (from s in Carriers
                         join ent in CoreEnt on s.CoreEntID equals ent.CoreEntID                       
                         where s.NeedsTransfer == True
                         select s).Single(); 
            //using Single()/FirstOrDefault() depends on what is the type of other objects. If it shows error to you than you can remove it
        }
        return result;
    }
    catch (Exception ex)
    {
        return result;
    }