LINQ query to get count of records - c#

I'm trying to retrieve some records from database along with a count, with LINQ.
DataTable dtByRecipe = (from tbrp in context.tblRecipeParents
join tbrc in context.tblRecipeChilds on tbrp.RecipeParentID equals tbrc.RecipeParentID
join tbp in context.tblProducts on tbrc.ProductID equals tbp.ProductID
join tbps in context.tblProductSales.AsEnumerable()
on tbp.ProductID equals tbps.ProductID
join tbs in context.tblSales.AsEnumerable()
on tbps.ProductSalesID equals tbs.ProductSalesID select new
{
tbrp.Recipe,
tbp.ProductID,
tbps.ProductSalesID,
tbrp.Yield,
Product = tbp.ProductCode + " - " + tbp.ProductDescription,
ProductYield = tbrp.Yield,
TotalYield = "XXX",
Cost = "YYY"
}).AsEnumerable()
.Select(item => new {
item.Recipe,
Count = GetCount(item.ProductID, item.ProductSalesID, context),
item.Yield,
Product = item.Product,
ProductYield = item.ProductYield,
TotalYield = "XXX",
Cost = "YYY"
}).OrderBy(o => o.Recipe).ToDataTable();
private int GetCount ( int ProductID, int ProductSalesID, MTBARKER_DBEntities context )
{
int query = ( from tbps in context.tblProductSales
join tbp in context.tblProducts on tbps.ProductID equals tbp.ProductID
join tbs in context.tblSales
on tbps.ProductSalesID equals tbs.ProductSalesID
where tbp.ProductID == ProductID && tbps.ProductSalesID == ProductSalesID
select tbs ).Count();
return query;
}
In above query I get the expected result but since there are around 10K records in the database it consumes a lot of time to produce the result. The issue is with the following approach I have used to get the count.
Count = GetCount(item.ProductID, item.ProductSalesID, context),
Is there any productive way that I could prevent this issue?

Well Stored Procedures is best choice for performance.Use Stored Procedures in the Entity Framework for selection and for reporting.

Related

Select with JOIN, Group BY and SUM in related 1:n tables (2nd level of child table) with LINQ / EF-Core

I have these tables which have 1:n and then 1:n relationship with each other:
How can I sum up the amount of Expenses for one specific household?
This is my SQL for that:
SELECT households.Id as HouseholdId,
households.Name HouseholdName,
SUM(expenses.Amount) as SumExpenses
FROM [Households] households
INNER JOIN Accounts accounts
ON households.Id = accounts.HouseholdId
INNER JOIn Expenses expenses
ON expenses.AccountId = accounts.Id
WHERE households.Id = '2AFAB095-39D6-4637-1FC1-08DAC249FA0A'
GROUP BY households.Id, households.Name;
This is what I have done so far:
var results = await (
from household in Context.Households
join account in Context.Accounts
on household.Id equals account.HouseholdId
join expense in Context.Expenses
on account.Id equals expense.AccountId
group new { household }
by new { household.Id, household.Name, AccountName = account.Name, Amount = expense.Amount}
into g
select new
{
HouseholdId = g.Key.Id,
HouseholdName = g.Key.Name,
AccountName = g.Key.AccountName,
//What to do here to get the sum?
}).ToListAsync();
LNQ grouping has the same behaviour as in the SQL. If you add additional grouping keys - you will fail. Important part is what to group.
Also I have removed other artifacts which are not present in your original SQL. You have test that adding additional grouping keys will not change result.
var householdId = ...;
var query =
from household in Context.Households
join account in Context.Accounts
on household.Id equals account.HouseholdId
join expense in Context.Expenses
on account.Id equals expense.AccountId
where household.Id == householdId
group expense
by new { household.Id, household.Name }
into g
select new
{
HouseholdId = g.Key.Id,
HouseholdName = g.Key.Name,
SumExpenses = g.Sum(x => x.Amount)
};

C# LINQ select from table and join multiple table and join a SQL query view in SQL server

I have a code that selecting from table and joining multiple tables and joining dbContext.Database.sqlQuery from view in sql server.
But it gives me this error
Unable to create a constant value of type
'ITManagement.Models.Employee'. Only primitive types or enumeration
types are supported in this context.
My code
public JsonResult getEmployeeAsset(EmployeeController employee)
{
var employeeID = Request.QueryString["employeeID"];
var devices = (from asset in db.Devices
where asset.EmployeeID == employeeID
join brand in db.DeviceBrands on asset.Brand equals brand.ID
join model in db.DeviceModels on asset.Model equals model.ID
join type in db.DeviceTypes on asset.DeviceType equals type.ID
join room in db.Rooms on asset.FullRoomCode equals room.FullCode
//if device has last employee
join lsEmp in db.Database.SqlQuery<LDAPUsers>("SELECT * FROM V_LDAP_Users") on asset.LastEmployeeID equals lsEmp.employeeID into lstEmp
join sysUser in db.AspNetUsers on asset.sscUser equals sysUser.Id
from lastEmployee in lstEmp.DefaultIfEmpty()
select new
{
deviceID = asset.ID,
SerialNumber = asset.SerialNumber,
Type = type.Type,
BrandName = brand.BrandName,
ModelName = model.ModelName,
MaccCode = asset.MaccCode,
PONumber = asset.PONumber,
WarrantyDate = asset.WarrantyDate.ToString(),
MacAddress = asset.MacAddress,
WIFIMacAddress = asset.WIFIMacAddress,
PCName = asset.PCName,
LastEmployee = asset.LastEmployeeID + "-" + lastEmployee.employeeName,
Shared = asset.Shared == 1 ? "True" : "False",
Location = room.RoomName,
RecordedBy = sysUser.Name,
requestID = (from request in db.StoreRequests where request.DeviceID == asset.ID && request.State == 1 && request.VoucherType == "ASD" orderby request.ID select request.ID).FirstOrDefault()
}).DefaultIfEmpty();
return Json(new { assets = devices == null ? null : devices }, JsonRequestBehavior.AllowGet);
}
Your help please, thanks.
First of all, have you tried nested queries by commenting them out?
for example;
//join lsEmp in db.Database.SqlQuery<LDAPUsers>("SELECT * FROM V_LDAP_Users") on asset.LastEmployeeID equals lsEmp.employeeID into lstEmp
//requestID = (from request in db.StoreRequests where request.DeviceID == asset.ID && request.State == 1 && request.VoucherType == "ASD" orderby request.ID select request.ID).FirstOrDefault()
If there is no problem in these two, you can quickly find out which one is causing the problem by commenting the fields.
Tip: Also, more than 3 joins will affect your query performance. Try to split your queries as much as possible.

MongoDb aggregate in c#

I need to join 3 collections and get the desired data. I have written a LINQ query which working fine but I want to write it using mongoDb's Aggregate interface.
Here is my query in LINQ.
var query = from user in userCollection.AsQueryable()
where user.Id == userId
from userDivision in user.Divisions
select new { userDivision.DivisionId } into divisions
join division in divisionCollection.AsQueryable() on divisions.DivisionId equals division.Id
select new { division.CompanyId } into divisionCompany
join company in companyCollection.AsQueryable() on divisionCompany.CompanyId equals company.Id
select new
{
company.Id,
company.Code,
company.Name,
company.Address,
ConcurrencyId = "1_" + "3_" + company.Id
} into c
join concurrency in concurrencyCollection.AsQueryable() on c.ConcurrencyId equals concurrency.Id into concurrencies
from concurrencyEmpty in concurrencies.DefaultIfEmpty()
select new Response.Company
{
Id = c.Id,
Code = c.Code,
Name = c.Name,
CodeAndName = c.Code + " - " + c.Name,
Address = c.Address,
ConcurrencyVersion = concurrencyEmpty.Version
};
Can anybody tell me how to write this query using MongoDb's aggregate interface (using LookUp etc).
Thanks in advance.

Grouping on virtual (predefined) column in linq

I am trying to write a linq query for one of my dashboard which retrieves data based on some conditions. I want to display records count based on the different status available in my table.
Following is the SQL query in which I am trying to convert into Linq.
select count(id) cnt, 'Available' label from table where date = sysdate
Above query is returning below result in DB IDE. This is the result I want with linq
cnt label
0 Available
I've tried with following linq query but it is returning 0 count and hence result is not being retrieved.
var data = (from a in context
where a.date = System.DateTime.Now
group a in a.id into g
select new {
cnt = g.Count(),
label = 'Available'
}).ToList();
How do I achieve above mentioned result in linq. Your help will be appreciated.
Thanks
-------------EDIT----------------
Updated LINQ query
var data = (from a in context.TableA
where a.id = uniqueID
group a in a.id into g
select new {
cnt = g.Count(),
label = 'Available'
}).Concat(from a in context.TableB
where a.id = uniqueID
group a in a.id into g
select new {
cnt = g.Count(),
label = 'WIP'
}).ToList();
To count the number of elements matching a predicate in a linq query simply use the where clause:
var results =
from a in context
where a.date.Date == DateTime.Now.Date
&& a.id == someIdHere
select a;
var data = new {
count = results.Count(),
label = "Available"
};
Or, in extension method syntax (which I prefer):
var results = context.Where(a => a.date.Date == DateTime.Now.Date && a.id == someIdHere);
var data = new {
count = results.Count(),
label = "Available"
};
Also be careful when comparing a DateTime object with regards to what results you desire; comparing DateTime.Now to a date will likley return false since it will compare the time code as well, use the DateTime.Date property to obtain only the date from the object for the purposes of comparison.

linq join code doesn't work

I have 2 records in tblMaterials and zero record in tblMaterialTenderGroups
But when I fetch the data to gridview it shows me the two records, and the join doesn't work
public List<tblMaterial> ShowPresentMaterialInGroup()
{
List<tblMaterial> q = (from i in dbconnect.tblMaterials.AsEnumerable()
join b in dbconnect.tblMaterialTenderGroups on i.materialId equals b.materialId
where b.MaterialGroupId == _materialGroupId
select new tblMaterial()
{
existAmout = i.existAmout,
materialId = i.materialId,
name = i.name,
needAmount = i.needAmount,
requestAmount = i.requestAmount,
unit = i.unit,
requestId = i.requestId
}).ToList();
return q;
}
Can you please try this
List<tblMaterial> q = from i in dbconnect.tblMaterials
join b in dbconnect.tblMaterialTenderGroups on i.materialId equals b.materialId
select new { existAmout = i.existAmout,
materialId = i.materialId,
name = i.name,
needAmount = i.needAmount,
requestAmount = i.requestAmount,
unit = i.unit,
requestId = i.requestId}.ToList();
May be using returning the two records..
I read something here
Using AsEnumerable will break off the query and do the "outside part"
as linq-to-objects rather than Linq-to-SQL. Effectively, you're
running a "select * from ..." for both your tables and then doing the
joins, where clause filter, ordering, and projection client-side.

Categories