Hello I've the following working code:
public async Task<object> GetSomeData(FiltersModel filtersModel)
{
IQueryable<DTOModel> dtoModel;
var query = from a in dbContext.Table1
join b in dbContext.Table2 on a.Id equals b.Id
join c in dbContext.Table3 on b.Id equals c.Id
where a.SomeColumn == filtersModel.SomeColumn
orderby a.DateTime descending
select new DTOModel
{
Table1Id = a.Id,
Table2Id = b.Id,
Table3Id = c.Id,
Table1Column = a.SomeColumn
};
// response => DTOModel object
var response = await query.ToListAsync();
//If this okay or there is another way to handle it?
// dtoModel => System.Collections.Generic.List
dtoModel = response.AsQueryable();
ApplySomeFilter(ref dtoModel, filterParameters.FilterByVisible);
}
private void ApplySomeFilter(ref IQueryable<DTOModel> dtoModel, int? isVisible)
{
if (isVisible < 0)
return;
dtoModel = dtoModel.Where(y => y.SomeColumn == isVisible).ToList().AsQueryable();
}
This code for the moment is working as expected. I'm able to retrieve my data from database but my first question is: if it's okay to use ToListAsync() and after that convert the data AsQueryable? or if exists a better and more efficient way to do it?
And as you can see the filtering also is working as expected but I'm creating a list and after that I'm converting the data AsQueryable too. So, this is also related to the above question I guess.
Below sample code should useful to you.
public IQueryable<DTOModel> GetAllItemLibrarys(string param1)
{
using (DBContext dbcontext = new DBContext ())
{
//create sql param for site param
var customParam = new SqlParameter("@Param1", param1);
//use sql to access
var items = dbcontext.DTOModel.SqlQuery("Select * from dbo.Table1 WHERE Param1 = @param1)", customParam).Select(i => i.Table1Id ==XX).ToList();
//use linq on local tablecontroller generated query against sql id results
return Query().Where(i =>i...));
}
}
Related Post: Custom IQueryable from EF SQL for Azure Mobile App Service
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.