我需要从两个联接表中带上数据。数据集将是什么类型?我是否需要创建一个包含该数据集类型的两个表中属性的类,或者可以使用实体模式中的类型。如何修改我的方法以正常工作?
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;
}
}
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;
}