linq to sql ExecuteQuery() as IQueryable

牧云@^-^@ 提交于 2020-02-03 09:16:40

问题


ExecuteQuery() method returns an IEnumerable but is there a way to make it return IQueryable?


回答1:


Well, you can call AsQueryable, but it won't do any good. The problem is that when you use ExecuteQuery, the query isn't composable because LINQ to SQL doesn't "understand" it as such.

One of the core purposes of IQueryable<T> is to allow the various aspects of a query to be composed together and then LINQ to SQL can convert them into a single SQL query. That just doesn't work when one of the bits of the query is effectively opaque.




回答2:


If you want, you can export the result of your query to a list, and next convert it to IQueryable. See the next example code:

public IQueryable<Data> GetData()
    string query = @"select ...";
    object[] parameters = new object[...]{...};
    var resultQuery = this.DataContext.ExecuteQuery<SICDB.Data>(query, parameters);
    var tempList = resultQuery .ToList();
    return tempList.AsQueryable();
}


来源:https://stackoverflow.com/questions/1065241/linq-to-sql-executequery-as-iqueryable

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