I have 4 tables
- User
- UserId
- UserName
- Device
- DeviceId
- DeviceTypeId (FK DeviceType)
- DeviceType
- DeviceTypeId
- UserDevice
- UserId (FK User)
- DeviceId (FK Device)
I would like to get list of devices that 1 user has, grouped by DeviceType. So result class should be something like this
class m
{
int DeviceTypeId;
List<Device> devices;
}
I did try few tricks in LINQ but didnt get desired result. It has been years since I used LINQ last time.
You were almost there with your comment:
var data = from d in db.Devices
join dt in db.DeviceType on d.DeviceTypeId equals dt.DeviceTypeId
join du in db.UserDevices on d.DeviceId equals du.DeviceId
where (du.UserId == currentUser)
group d by d into g
select new m
{
DeviceTypeId = g.Key.DeviceTypeId,
devices = (from d in g select d).ToList()
};
If you add public string TypeName { get; set; } to your m class, you can populate it with this query:
var data = from d in devices
join dt in deviceTypes on d.DeviceTypeId equals dt.DeviceTypeId
join du in userDevices on d.DeviceId equals du.DeviceId
where (du.UserId == 1)
group d by dt into g
select new m
{
DeviceTypeId = g.Key.DeviceTypeId,
TypeName = g.Key.TypeName,
devices = (from d in g select d).ToList()
};
I guess you want something like this:
return
userDevices
.Where(ud => ud.UserId == myUserId)
.SelectMany(ud => devices.Where(d => d.DeviceId == ud.DeviceId))
.GroupBy(d => d.DeviceTypeId)
.Select(g => new m { DeviceTypeId = g.Key, devices = g.ToList() });
of course I cannot test this but it should be about right (minus some spelling problems)
BTW: I did not even try to give you some ORM like syntax as I have no clue what you are using here - this might be a rather poor performing solution but you should get the idea ...
Related
im required to make a query to db to fetch data fro a highchart widget in our site here is the code I use currently,
var highChartsData =
(from a in db.roomdetails
join b in db.ApplySchedule on a.RoomId equals b.RoomID
where b.Status == true
select new HighlinePie
{
Title = a.RoomName,
Date = b.MDate,
Value = db.ApplySchedule.Where(x => x.RoomID == a.RoomId).GroupBy(x=>x.MDate).Count(),
}).ToList();
The problem with this approach is right now get the total count but what i need is the count based on date, for example if there was two entry on date 12/09/20201 and three entry on 14/09/20201 the data should be "Title,12/09/20201,2","Title,14/09/20201,3".
You have to use grouping:
var groupQuery =
from a in db.roomdetails
join b in db.ApplySchedule on a.RoomId equals b.RoomID
where b.Status == true
group b by new { a.RoomName, b.MDate } into g
select new HighlinePie
{
Title = g.Key.RoomName,
Date = g.Key.MDate,
Value = g.Count()
};
var highChartsData = groupQuery.ToList();
I'm trying to rewrite sql query to linq but can't do it myself.
The most problem for me is to get I,II and III aggregated values.
Sql query:
select o.Name,t.TypeID, SUM(e.I),SUM(e.II),SUM(e.III) from Expenditure e
join Finance f on f.FinanceId = e.FinanceId
join FinanceYear fy on fy.FinanceYearId = f.FinanceYearId and fy.StatusId = 1
join Project p on p.ProjectId = fy.ProjectId
join Organization o on o.OrganizationId = p.OrganizationId
join Type t on t.TypeID = p.TypeID
where fy.Year = 2018
group by o.Name,s.TypeID
and what I have done so far is:
var x = (from e in _db.Expenditures
join f in _db.Finances on e.FinanceId equals f.FinanceId
join fy in _db.FinanceYears on f.FinanceYearId equals fy.FinanceYearId and fy.StatusId = 1 // this does not work, cant join on multiple conditions?
join p in _db.Projects on fy.ProjectId equals p.ProjectId
join o in _db.Organizations on p.OrganizationId equals o.OrganizationId
join s in _db.Types on p.TypeId equals s.TypeId
group new { o, s } by new { o.OrganizationId, s.TypeId }
into grp
select new AggModel
{
OrganizationId = grp.Key.OrganizationId,
TypeId = grp.Key.TypeId,
I = ?,
II = ?,
III = ?,
}
);
Try something like this:
group new { e, o, s } by new { o.OrganizationId, s.TypeId }
into grp
select new AggModel
{
OrganizationId = grp.Key.OrganizationId,
TypeId = grp.Key.TypeId,
I = grp.Sum(a => a.e.I),
II = grp.Sum(a => a.e.II),
III = grp.Sum(a => a.e.III),
}
You'll need to adjust the right side of the lambda to navigate to the correct property.
You Need to use the Group by for aggregation methods.
Check the below link for more Knowledge.
How to use aggregate functions in linq with joins?
In the above diagram for each customer I want to select all orders and then for each order I have calculate a TotalPrice = (Sum of all Food Items included in order * Quantity) + ExtraPrice. I am struggling to create a query for it using linq to sql.
var res = (from a in dc.orders
join b in dc.orderLines on a.orderId equals b.fk_orderId
join c in dc.foodItems on b.fk_foodId equals c.foodId
where a.fk_custId == cID
group new { a,c,b } by a into g
select new
{
OID1 = g.Key.orderId,
date1 = g.Key.date.Value.Date,
price1 = g.Sum(x => x.c.price * x.b.quantity) + g.Key.orderLines.Select(o => o.extraPrice).Sum()
});
Given above is linq query I was looking for.
Should be something close to this. I'm not around a computer to test so let me know if you get errors.
db.orders.Select(o => new { o.orderid, o.date, TotalPrice = ( (o.orderLines.Select(ol => ol.food items.Count()).Sum() * o.Quantity) + o.extraPrice) } )
I try to translate this SQL code :
SELECT w.Id, w.LastName, w.FirstName, SUM(d.Price*dt.Number) AS somme
FROM Waiter w
INNER JOIN Client c on w.Id = c.WaiterId
INNER JOIN DisheOnTable dt on c.Id = dt.ClientId
INNER JOIN Dishe d on dt.DisheId = d.Id
GROUP BY w.Id, w.LastName, w.FirstName
ORDER BY somme DESC;
in entity framework.
I tried something like this
var query2 = (from w in db.Waiter
join c in db.Client on w.Id equals c.WaiterId
join dt in db.DisheOnTable on c.Id equals dt.ClientId
join d in db.Dishe on dt.DisheId equals d.Id
group w by new { w.Id, w.LastName, w.FirstName } into g
//orderby g.Select() descending
select new
{
id = g.Key.Id,
lastname = g.Key.LastName,
firstname = g.Key.FirstName,
total = g.Sum(q => q.)
});
but my sum doesn't work (after multiple research and try) and i don't know how to multiply my variables.
PS : The SQL statement works well, i tried it.
Thank you for helping guys ! :)
You need to group on both dish and DishOnTable alias as Price is in Dish and Number is in DishOnTable:
group new{ d,dt} by new {w.Id, w.LastName, w.FirstName} into g
and now sum the columns which you want from it
select new {
id = g.Key.Id,
lastname = g.Key.LastName,
firstname = g.Key.FirstName,
total = g.Sum(q => q.d.Price * q.dt.Number)
}).OrderBy(x=>x.total)
Hi i am having difficulties to group these data as it has no aggregate function. I have the following data in 2 tables and i would like to Join both by PaymentId and display only 1 row of record.
Question:
How do i display the final result in only 1 ROW groupby CoursePaidForMonthYear.
I would like to get all data from Payment.*, TutorCourseCommissions.* and CoursePaidForMonthYear column in same row displaying (September, October, November)
Example:
referenceId| TutorId| CoursePaidForMonthYear |
1 | 1019 | September, October, November OR 9,10,11|
My work:
var result = from u in db.Users
join p in db.Payments on u.Id equals p.UserId
join tt in db.TutorCourseCommissions on p.Id equals tt.PaymentId into gtt
from tt in gtt.DefaultIfEmpty()
where u.Id == user.Id
GroupBy tt.CoursePaidForMonthYear ??
select new { u, p, tt };
foreach (var r in result)
{
Payment payment = new Payment();
payment.MonthToPay = (r.tt == null) ? null : common.GetMonthName(r.tt.CoursePaidForMonthYear.Month, true);
payment.Amount = r.p.Amount;
output.Add(payment);
}
return output;
What you need to do is group the months by the user and payment, and then perform an aggregation on the grouped months. In this case I used String.Join() to combine the distinct months coming from the commission.
This will give you results along the lines of { User = "John Doe", Payment = ..., Months = "January, February" }
var result = from u in db.Users
where u.UserID == user.Id
join p in db.Payments on u.Id equals p.UserId
join comm in db.TutorCourseCommissions
on p.Id equals comm.PaymentId
group common.GetMonthName(comm.CoursePaidForMonthYear,true)
by new { User = u, Payment = p } into g
select new
{
User = g.Key.User,
Payment = g.Key.Payment,
//...select out other properties here...
Months = String.Join(", ", g.Distinct())
};
Made a class with properties regarding all columns you required and then do the selection using its instance ...
Like
public TutorPayments
{
public int UserID{
get;
set;
}
public int PayType{
get;
set;
}
public int TutorID{
get;
set;
}
//and so on all columns property that you required
}
now you can get all these in your query by creating its instance in this way
var result = from u in db.Users
join p in db.Payments on u.Id equals p.UserId
join tt in db.TutorCourseCommissions on p.Id equals tt.PaymentId into gtt
from tt in gtt.DefaultIfEmpty()
where u.Id == user.Id
GroupBy tt.CoursePaidForMonthYear ??
select new TutorPayments {UserID = u.id,PayType = p.id,TutorID = tt.TutorId, ............ };