Client side GroupBy is not supported - fix - c#

How to fix this query to meet the new .NET Core 3 update?
var rtn =
from fav in _context.AdvertFavorite
join advert in _context.Adverts on fav.AdvertID equals advert.AdvertID
join blob in _context.Blobs on advert.AdvertID equals blob.AdvertID
join user in _userManager.Users on advert.UserID equals user.Id
where fav.UserID == usrid
group new { blob, user, advert } by new { advert.AdvertID } into grp
select grp;
var rtn1 = rtn.ToList();
return rtn1;

Related

SQL Server query to C# Linq

I'm currently having a rough time converting my SQL query to LINQ for a school project I'm using WPF and Entity Framework
here is my SQL query (working exactly as I expect)
select IngrediantName,sum(IngrediantQuantity) as Quantity, IngrediantMeasurementUnit from Users
join Shopping_List
on Users.UserID = Shopping_List.ShoppingListID
join List_Item
on List_Item.ShoppingListID = Shopping_List.ShoppingListID
join Ingrediant
on Ingrediant.IngrediantID = List_Item.IngrediantID
where Users.UserID = 1
group by IngrediantName,IngrediantMeasurementUnit
Here is the query that I have so far
var query = from user in dbContext.Users
join shoppingList in dbContext.ShoppingLists on user.UserId equals shoppingList.UserId
join listItem in dbContext.ListItems on shoppingList.ShoppingListId equals listItem.ShoppingListId
join ingrediant in dbContext.Ingrediants on listItem.IngrediantId equals ingrediant.IngrediantId
where currentUserNumber == user.UserId
select new
{
name = ingrediant.IngrediantName,
quantity = ingrediant.IngrediantQuantity,
unit = ingrediant.IngrediantMeasurementUnit,
};
Here is what i try so far
var query = from user in dbContext.Users
join shoppingList in dbContext.ShoppingLists on user.UserId equals shoppingList.UserId
join listItem in dbContext.ListItems on shoppingList.ShoppingListId equals listItem.ShoppingListId
join ingrediant in dbContext.Ingrediants on listItem.IngrediantId equals ingrediant.IngrediantId
where currentUserNumber == user.UserId
group ingrediant by ingrediant.IngrediantQuantity into x
select new
{
name = x.GroupBy(x => x.IngrediantName),
quantity = x.Sum(x => x.IngrediantQuantity),
unit = x.GroupBy(x => x.IngrediantMeasurementUnit),
};
this one return the following error wiches doesn't tell much
Argument type 'System.Linq.IQueryable1[System.Linq.IGrouping2[System.String,Microsoft.EntityFrameworkCore.Query.TransparentIdentifierFactory+TransparentIdentifier2[Microsoft.EntityFrameworkCore.Query.TransparentIdentifierFactory+TransparentIdentifier2
If someone could point me in the right direction I would appreciate it, if you need more info I will provide it for sure
Thanks
UPDATE
I got this working here the answers to the question for anyone else
var query =
from user in dbContext.Users
join shoppingList in dbContext.ShoppingLists
on user.UserId equals shoppingList.UserId
join listItem in dbContext.ListItems
on shoppingList.ShoppingListId equals listItem.ShoppingListId
join ingrediant in dbContext.Ingrediants
on listItem.IngrediantId equals ingrediant.IngrediantId
where currentUserNumber == user.UserId
group ingrediant by new { name = ingrediant.IngrediantName, unit = ingrediant.IngrediantMeasurementUnit } into x
select new
{
name = x.Key.name,
quantity = x.Sum(x => x.IngrediantQuantity),
unit = x.Key.unit,
};
if you look at the group line
group ingrediant by new { name = ingrediant.IngrediantName, unit = ingrediant.IngrediantMeasurementUnit } into x
from my understanding you use the new to create a new selector then you can use x.key.name and x.key.unit where name and unit are simply some variable

i need to get all rows from table 1 even when there is no info related in second table using Linq C#

I have this code that shows all tickets with its reply info (if any) and it is working fine. The problem is when there is no reply for ticket found in ticket reply, the whole ticket info row from support ticket doesn't appear. So I want to show all rows in (Support_Teckets table) whether or not it has related data in the (Ticket_Reply table)
var query = (from st in Db.Support_Teckets
join rp in Db.Ticket_Reply.GroupBy(m => m.Support_Tecket_Id)
.Select(m => m.OrderByDescending(x => x.Date).FirstOrDefault())
on st.Support_Tecket_Id equals rp.Support_Tecket_Id
into g
from rp in g.DefaultIfEmpty()
join tr in Db.trainers on rp.trainer_id equals tr.trainer_id
join pr in Db.Technical_problem on st.Technical_problem_Id equals pr.Technical_problem_Id
select new SupportTicketsDetails
{
Support_Tecket_Id = st.Support_Tecket_Id,
Created_Date = st.Created_Date,
Created_Time = st.Created_Time,
Order_by = Db.trainers.Where(b => b.trainer_id == st.trainer_id)
.FirstOrDefault().trainer_name,
Technical_problem_name = pr.Technical_problem_name,
Created_details = st.Created_details,
Location = st.Location,
Technician = Db.trainers.Where(b => b.trainer_id == st.Technician_Id)
.FirstOrDefault().trainer_name,
Is_Closed = rp.Reply_Text,
Closing_Date = st.Closing_Date ,
Last_replier = Db.trainers.FirstOrDefault(a => a.trainer_id == rp.trainer_id)
.trainer_name.ToString(),
Last_reply_Date = rp.Date,
Last_reply_Time = rp.Time,
points = st.points
}).ToList().OrderByDescending(a => a.Created_Date);
return View(query);
I thank you for your cooperation in advance
It's really not easy to test, but I suspect you need something like this:
from st in Db.Support_Teckets
join pr in Db.Technical_problem on st.Technical_problem_Id equals pr.Technical_problem_Id
join x in
from rp in Db.Ticket_Reply.OrderByDescending(z => z.Date).Take(1)
join tr in Db.trainers on rp.trainer_id equals tr.trainer_id
select new { rp, tr }
on st.Support_Tecket_Id equals x.rp.Support_Tecket_Id into gxs
from gx in gxs.DefaultIfEmpty()
select new

LINQ to SQL (insert into fact table based on 3 tables)

I am not able to work with the below join.
Idea is to find any new (AumProductId and IdClient) combinations from AceBiMonthlyDaos to create entry in Investment table with Unique Id, if those AumProductId and IdClient exist in Reference table of Instrument and Financial Account respectively.
var result = (from AceBi in context.AceBiMonthlyDaos
join Inst in context.Instruments
on AceBi.AumProductId equals Inst.AceId
join FinAcct in context.FinancialAccounts
on AceBi.IdClient equals FinAcct.AceClientId
join Invst in context.Investments
on new { Inst.Id, FinAcct.Id } equals { Invst.InstrumentId, Invst.FinaAcctId} into pair
from InvestmentDao in pair.DefaultIfEmpty()
var query = (from AceBi in context.AceBiMonthlyDaos
join Inst in context.Instruments on AceBi.AumProductId equals Inst.AceId
join FinAcct in context.FinancialAccounts on AceBi.IdClient equals FinAcct.AceClientId
join Invst in context.Investments on new { instrumentId = Inst.Id, finAccountId = FinAcct.Id } equals new { instrumentId = Invst.InstrumentId, finAccountId = Invst.FinAcctId }
into pair
from Invst in pair.DefaultIfEmpty()
select new { Inst, FinAcct, Invst }).Distinct();

Linq to SQL - Right Join [duplicate]

This question already has answers here:
LINQ Left Join And Right Join
(4 answers)
Closed 9 years ago.
I'm trying to convert this query (Already working)
select [User].ID AS ID_USER
from [User]
right join Building on Building.ID = 4 AND
Building.ID_USER_RESPONSIBLE <> [User].ID
where [User].ID_MANAGER = 1
To Linq to SQL, but I don't know what I'm doing wrong. Look at my trying
IList<User> lstUser = (from building in db.GetTable<Building>()
join user in db.GetTable<User>()
on new
{
ID_BUILDING = building.ID,
ID_USER = building.ID_USER_RESPONSIBLE
}
equals new
{
ID_BUILDING = 4,
ID_USER != user.ID
} into grpUser
from grp in grpUser.DefaultIfEmpty()
where building.ID_MANAGER = 1
select new
{
ID_USER =
});
I need to return all Users plus the Users which aren't responsible to any Building;
EDIT
Any of solution posted here worked.
I've resolved just doing this:
var lstUser = (from building in db.GetTable<Building>()
join user in db.GetTable<User>()
on building.ID equals 4
where user.ID_MANAGER == 1 &&
building.ID_USER_RESPONSIBLE != user.ID
select new
{
ID_USER = user.ID,
NAME = user.NAME
}).ToList();
See if the following works:
IList<User> lstUser = (from building in db.GetTable<Building>()
join user in db.GetTable<User>()
on new
{
ID_BUILDING = building.ID,
ID_USER = building.ID_USER_RESPONSIBLE,
building.ID_MANAGER
}
equals new
{
ID_BUILDING = 4,
ID_USER = user.ID,
1
} into grpUser
from grp in grpUser.DefaultIfEmpty()
select new
{
ID_USER = grp.ID
});
That being said, there seems to be a gap in your requirement, "I need to return all Users plus the Users which aren't responsible to any Building;" I may be mis-reading your statement but isn't the list of users that aren't responsible to a building a subset of the larger "All Users" set. As a result, you would always just get All Users regardless of their building membership.

Convert stored procedure to LINQ

I'm using MVC3 and still learning LINQ. I'm having some trouble trying to convert a query to LINQ to Entities. I want to return an Json method
My stored procedure
Create Procedure [dbo].[ResourceReports]
(
#EmployeeID int
)
as
begin
select p.projectName AS Projects, count( b.[CreatedByID]) AS Bugs
from [EmployeeDetails] e inner join [Bugs] b on e.[EmployeId] = b.[CreatedByID]
inner join Projects p on b.ProjectId = p.ProjectId
where e.[EmployeId] = #EmployeeID
group by P.projectName
end
What I have is a few tables, I started writing this out in LINQ but I'm not sure how to properly return the correct type or cast this.
My controller
public JsonResult Getchart()
{
var Bug = db.Bugs.ToList<Bug>();
var EmployeDetails = db.EmployeeDetails.ToList<EmployeeDetail>();
var projects = db.Projects.ToList<Project>();
var result = (from e in EmployeDetails
join b in Bug on e.EmployeId equals b.CreatedByID
join p in projects on b.ProjectId equals p.ProjectId
where e.EmployeId = #EmployeId
group p.projectName
select new (p.projectName as Project ,count(b.CreatedByID) as Bug)).Take(50);
return Json(result,JsonRequestBehavior.AllowGet);
}
How will I pass the parameter to for the query, want the data to be returned in json format.
Assuming you can pass the value in as a parameter to the method:
public JsonResult Getchart(int employeeId)
{
var Bug = db.Bugs.ToList<Bug>();
var EmployeeDetails = db.EmployeeDetails.ToList<EmployeeDetail>();
var projects = db.Projects.ToList<Project>();
var result = (from e in EmployeeDetails
join b in Bug on e.EmployeeId equals b.CreatedByID
join p in projects on b.ProjectId equals p.ProjectId
where e.EmployeeId == employeeId // <-- use the parameter here
group p by p.projectName into g
select new {
Project = g.Key,
Bug = g.Count()
}
).Take(50);
return Json(result,JsonRequestBehavior.AllowGet);
}
BTW I intentionally corrected a few spellings of Employee
If this is a controller action, you would probably want to pass the ID via the URL. Also, there is no need to call ToList on your tables before querying, do the query at the database and only pull down the results e.g.
public JsonResult GetChart(int employeeId)
{
var query = (from e in db.EmployeeDetails
join b in db.Bugs on e.EmployeeId equals b.CreatedById
join p in db.Projects on b.ProjectId equals p.ProjectId
where e.EmployeeId == employeeId
group new {p, b} by new {
p.ProjectName
} into g
select new {
Project = g.Key.Name,
Bugs = g.Count()
}).Take(50);
return Json(query.ToList(), JsonRequestBehaviour.AllowGet);
}
Is this what you need:
public JsonResult Getchart(int employeId)
{
var Bug = db.Bugs.ToList<Bug>();
var EmployeDetails = db.EmployeeDetails.ToList<EmployeeDetail>();
var projects = db.Projects.ToList<Project>();
var result = (from e in EmployeDetails
join b in Bug on e.EmployeId equals b.CreatedByID
join p in projects on b.ProjectId equals p.ProjectId
where e.EmployeId == employeeId
group p.projectName
select new (p.projectName as Project ,count(b.CreatedByID) as Bug)).Take(50);
return Json(result,JsonRequestBehavior.AllowGet);
}
Are you sure you want to do all of those "ToList<>()" calls? Once you call "ToList<>()", you bring all three of those tables into memory from the database. If they are large, that could be a performance issue.
public JsonResult GetChart()
{
//int employeeId
var Bug = db.Bugs.ToList<Bug>();
var EmployeDetails = db.EmployeeDetails.ToList<EmployeeDetail>();
var projects = db.Projects.ToList<Project>();
var query = (from e in EmployeDetails
join b in Bug on e.EmployeId equals b.CreatedByID
join p in projects on b.ProjectId equals p.ProjectId
where e.EmployeId == 1
group new { p, b } by new
{
p.projectName
} into g
select new ChartModel
{
ProjectName = g.Key.projectName,
bug = g.Count()
}).ToList();
return Json(query, JsonRequestBehavior.AllowGet);
}
i Got ...

Categories

Resources