Need to get total counts of a record from table in database using ASP.NET Core Web API - asp.net-core

I need to count the total rows of a table when I pass a date as a parameter.
IService:
Task<int?> ScoresUpdated(DateTime date);
Service implementation:
public async Task<int?> ScoresUpdated(DateTime date)
{
var result = await _eventScheduleContext
.ViewPersonEventScore
.Where(x => x.DateTimeUtc.Date == date
&& x.ExamModeId == 1
&& x.EventStatusId == 8).ToListAsync();
return result.Count();
}
Controller:
public async Task<IActionResult> ScoresUpdated(DateTime date)
{
var result = await _examDetailService.ScoresUpdated(date);
return Ok(result);
}
But the output I'm getting is 0. In the database, I can see 80 rows.
Is there anything wrong with my code? Can you give me some suggestions how to fix it?

Suspect that it is due to this line
x.DateTimeUtc.Date == date
In SQL expression, I believe it translated to
CONVERT(date, DateTimeUtc) = #date
which #date is DateTime type. So this results that you are unable to query any records.
Use date.Date so that the translated SQL expression will be:
CONVERT(date, DateTimeUtc) = CONVERT(date, #date)
public async Task<int?> ScoresUpdated(DateTime date)
{
var result = await _eventScheduleContext
.ViewPersonEventScore
.Where(x => x.DateTimeUtc.Date == date.Date
&& x.ExamModeId == 1
&& x.EventStatusId == 8)
.ToListAsync();
return result.Count();
}
While seems you are only returning the count of the query result, you may consider applying CountAsync<TSource>(IQueryable<TSource>, CancellationToken).
This would improve the query performance as it will return the count, but not return all columns to the result.
public async Task<int?> ScoresUpdated(DateTime date)
{
return await _eventScheduleContext
.ViewPersonEventScore
.CountAsync(x => x.DateTimeUtc.Date == date.Date
&& x.ExamModeId == 1
&& x.EventStatusId == 8);
}

I don't see a reason for defining optional return type for the task (Task<int?>). I suggest using Task. refer to this Microsoft documentation for further details.
However, despite having 80 rows or entries in the DB as mentioned, you need to check which one of them match the condition you have defined (Where(x => x.DateTimeUtc.Date == date.Date
&& x.ExamModeId == 1
&& x.EventStatusId == 8))
If no entry meets the condition, the count of course would be zero.
Try removing the condition and check.

Related

How to combine IQueryable records in .net Core 6 Web API

I'm using .net Core 6 web API, and I've this case where I need to loop through multiple object, and each has a IQueryable result.
Then all results need to be grouped in one query. Finally the the result will be sent as paginated list.
Here's more details in the code, I will get list of ObjectA from db:
var query = _context.ObjectA.AsQueryable();
Now I need to get list of User. For some reason the users will have multiple records with their username, so each record represent a role:
var rolesFromDb = await _context.User.Include(x => x.Role).Where(x => x.Username == "user").ToListAsync();
Now I need to loop through dynamic roles and filter the query. I have 2 (or more) conditions, the the code will look like this:
if (rolesFromDb.Count != 0)
{
var result = new List<ObjectA>();
foreach (var item in rolesFromDb)
{
if (item.Role.Level == 1)
{
var condition1 = query.Where(x => x.Level == 1);
result.AddRange(condition1);
continue;
}
if (item.Role.Level == 2)
{
var condition2 = query.Where(x => x.Level == 2);
result.AddRange(condition2);
continue;
}
}
query = result; // I need to do something like this, this is not the way to do ofc
}
After this loop, I've a search filter that depends on this query.
In general, my main issue is I'm not able to make query = all filtered queries.
Thank you.

Easiest way to check record is exist or not in Linq to Entity Query

in store procedure we can check record is exist or not using following query for fast performance
if EXISTS ( Select 1 from Table_Name where id=#id )
But what about Linq query.
right now i have to store whole data in object like this
UserDetail _U=db.UserDetails.where(x=>x.id==1).FirstOrDefault();
Any Solution?
Use Linq's Any ie bool exist = db.UserDetails.Any(x => x.id == 1);
if(db.UserDetails.Any(x => x.id == 1)) {
var userDetail = db.UserDetails.FirstOrDefault(x => x.id == 1);
}
bool exist = db.UserDetails.Where(x=>x.id==1).Any();
if(exist){
//your-code
}else{
//your-code
}
Just check
if(_U == null)
This way you will get what you want in single query and you not need to execute addition query like
db.UserDetails.Any(x => x.id == 1)
I think, it does not require to fire two query. It can be accomplish by single query.
UserDetails objUserDetails =db.UserDetails.FirstOrDefault(x => x.id == 1);
if(objUserDetails==null)
{
// Do something
}
else
{
// do something with objUserDetails
}
var qry = db.User_Detail.Where(x => x.User_Id == 1).FirstOrDefault();
if(qry !=null)
{
// Do something
}
else
{
return false;
}
Try This...

Entity Framework Union And Paging

I am getting the result query by running two queries and joining them with union clause. I have to use paging but I need results as inserted line. I don't want to order it again. But when I use paging I have to use order by clause. how can I get results with natural order.
var prodFullTextQuery = _db.Products
.Where(x => !x.IsDeleted
&& x.ProdStatusValue == (int)ProdStatus.Active
&& (x.ProdName.Contains(searchText)
|| x.Keywords.Contains(searchText)
|| x.ManufacturerCode.StartsWith(searchText)
));
IQueryable<Product> prodQuery;
prodQuery = _db.Products.Where(x => !x.IsDeleted
&& x.ProdStatusValue == (int)ProdStatus.Active
&& x.VariantMainProdId == x.ProdId);
foreach (var s in searchText.Split(' '))
{
var temp = s;
prodQuery = prodQuery.Where(x => x.Keywords.Contains(temp));
}
prodFullTextQuery = prodFullTextQuery.Union(prodQuery)
//i need here by the order in which rows are inserted
var results= prodFullTextQuery
.OrderBy(x=>x.OrderId) //i don't want this
.Skip(prodCountPerPage * pageNumber)
.Take(prodCountPerPage)
.ToList()

How to append sql to Entity Framework query

How to append raw sql e.g. condition to entity framework linq query?
I'm open to all suggestions so it's not mandatory to append it to linq query main point is to e.g. filter by adding some custom sql to query overall.
Using IQueryable we can append the condition on the Linq to entity query.
private IQueryable<Customer> FilterList(IQueryable<Customer> customer, List<string> filter, string filterValue)
{
IQueryable<Customer> query = new List<Customer>().AsQueryable();
if (filter == null || string.IsNullOrEmpty(filterValue))
{
return customer;
}
var filterLower = filter.First().ToLower();
filterValue = filterValue.ToLower();
if (filterLower.Contains("retail") || (filterLower.Contains("distributor"))
{
return customer.Where(x => (!string.IsNullOrEmpty(x.Retail) && x.Retail.Contains(filterValue)) || (!string.IsNullOrEmpty(x.Distributor) && x.Distributor.Contains(filterValue)));
}
if (filterLower.Contains("retail"))
{
query = customer.Where( x=> !string.IsNullOrEmpty(x.Distributor) && x.Distributor.Contains(filterValue));
}
if (filterLower.Contains("distributor"))
{
query = customer.Where(x => !string.IsNullOrEmpty(x.Retail) && x.Retail.Contains(filterValue)).Union(query);
}
return query;
}

How to create a Dynamic Query when there's a condition that won't use all fields in the where section with LinQ

I am using a Data Entity from the db, and a Domain Service.
I am using .net's generated code for the simple queries, like this
public IQueryable<employee> GetEmployeesByLocale(int localeID)
{
return ObjectContext.employees.Where(e => e.Locale_ID == localeID);
}
Now, I need to change the .Where section accordingly, so:
if (localeID > 0)
{
['Where' should be like .Where(e => e.Locale_ID == localeID)];
}
if (projectID > 0)
{
[IF localeID == 0, 'Where' should be like .Where(e => e.Project_ID == projectID)
Else if localeID > 0, Where should use both, sort of .Where(e => e.Locale_ID == localeID && e.Project_ID == projectID)];
}
and so on with other variables.
There are many possible combinations , which is why I was trying to use the overload for .Where(string, parameter[])
string q = string.Empty;
if (localeID > 0)
{
q = "Locale_ID = " + localeID.ToString();
}
if (projectID > 0)
{
q = q == string.Empty ? "Project_ID = " + projectID.ToString() : q + " and " + "Project_ID = " + projectID.ToString();
}
... (for other variables and fields)
...
System.Data.Objects.ObjectParameter[] param = new System.Data.Objects.ObjectParameter[1];
param[0] = new System.Data.Objects.ObjectParameter("param", 1);
return ObjectContext.employees.Where(q, param);
However, this only gives an error, because then all the fieldnames are supposedly out-of-scope/non-existing. Even if use employees.[field_name] in the string, they "don't exist"
Does anyone know of a way to use conditionals inside the .Where part of the query? Or how to create some var or object containing my query so I can just parse it?
You can use
IQueryable<Employee> emp = ObjectContext.employees;
if (localeID > 0)
emp=emp.Where(e => e.Locale_ID == localeID).AsQueryable();
if (projectID > 0)
emp=emp.Where(e => e.Project_ID == projectID).AsQueryable();
This will concatenate the where clauses