问题
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