我有以下两种方法:
public IEnumerable<MyModel> GetDetails()
{
var Results1 = GetFromSql(typeof(MyModel),
"SELECT t1.field1,t2.field2,t3.field3,t4.field4 " +
"FROM table1 t1 " +
"INNER JOIN table2 t2 ON t1.id=t2.id " +
"INNER JOIN table3 t3 ON t1.id=t3.id " +
"INNER JOIN table4 t4 ON t1.id=t4.id ");
var Results2 = (from t1 in context.repository.Get()
join t2 in context.repository.Get() on t1.id equals t2.id
join t3 in context.repository.Get() on t1.id equals t3.id
join t4 in context.repository.Get() on t1.id equals t4.id
select new MyModel()
{
field1 = t1.field1,
field2 = t2.field2,
field3 = t3.field3,
field4 = t4.field4}).ToList();
});
return Results2;
}
private dynamic GetFromSql(System.Type returnType, string strSql)
{
var items = _context.Database.SqlQuery(returnType, strSql);
return items;
}
目前,Result2作为System.Collections.Generic.List返回,Result1作为动态返回(System.Data.Entity.Infrastructure.DbRawSqlQuery)。
如何让Results1返回与Results2完全相同的List?
我尝试使用typeof(List)代替typeof(MyModel),但这没有用。
(我知道我应该只使用Results2,但我有我的理由。)