MVC Entity Framework with Raw SQL

主宰稳场 提交于 2020-01-06 14:34:13

问题


I have the following two methods:

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;
}

Currently, Result2 is returned as a System.Collections.Generic.List and Result1 is being returned as a dynamic (System.Data.Entity.Infrastructure.DbRawSqlQuery).

How can I get Results1 to return a List exactly the same as Results2?

I have tried using typeof(List) in place of typeof(MyModel), but this didn't work.

(I know it would seem that I should just use Results2, but I have my reasons.)

来源:https://stackoverflow.com/questions/49539725/mvc-entity-framework-with-raw-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!