C#IQueryableto使用Linq表达式和类将IEnumerable转换为IEnumerable
我有一个类,它应该使用连接从数据库获取查询结果 我的班级是:C#IQueryableto使用Linq表达式和类将IEnumerable转换为IEnumerable,c#,entity-framework,linq,linq-to-sql,linq-to-entities,C#,Entity Framework,Linq,Linq To Sql,Linq To Entities,我有一个类,它应该使用连接从数据库获取查询结果 我的班级是: ` public class StepTwo { public int Id { get; set; } public string Party { get; set; } public string Currency { get; set; } public string Account { get; set; } public doub
`
public class StepTwo
{
public int Id { get; set; }
public string Party { get; set; }
public string Currency { get; set; }
public string Account { get; set; }
public double? Amount { get; set; }
}
`
然后,我创建了一个将返回结果的方法:
public IEnumerable<StageTwo> StepTwo()
{
var queryJoin = (from inn in db.Input.Take(10)
join yacc in db.AccY on inn.Action equals yacc.Action
orderby inn.Id descending
select new
{
inn.Id,
inn.XParty,
inn.Curr,
yacc.Action,
inn.Amount
});
return queryJoin;
}
我知道类名与数据库名有点不同,但我已尝试将它们更改为匹配。根据上面的错误,我认为是别的原因
任何建议都将不胜感激,谢谢。您的错误消息告诉您,
queryJoin
是一种匿名类型,但您试图将其强制为IEnumerable
,而C#无法进行转换
之所以queryJoin
是匿名类型,是因为您没有在linq查询中定义它。所以要解决它,而不是这个:
select new
{
inn.Id,
inn.XParty,
inn.Curr,
yacc.Action,
inn.Amount
});
你会想要这个:
select new StepTwo
{
Id = inn.Id,
Party = inn.XParty,
Currency = inn.Curr,
Account = yacc.Action,
Amount = inn.Amount
});
为什么选择匿名类而不是
selectnewstagewo{Id=inn.Id…}
?谢谢,这就解决了问题!仍然在努力解决这个问题,这是一个很大的帮助。保重,巴德。乐意帮忙:)
select new StepTwo
{
Id = inn.Id,
Party = inn.XParty,
Currency = inn.Curr,
Account = yacc.Action,
Amount = inn.Amount
});