I have the following table (Records):
RecordID int,
Nickname nvarchar(max),
DateAdded datetime
I need group by max count of records for Nickname. I made it:
var users = (from i in db.Records
where i.Form.CompetitionID == cID
group i by i.Nickname into g
orderby g.Count() descending
select new TopUserModel()
{
Nickname = g.Key,
Position = g.Count()
}).Take(100).ToList();
it works
Right now I need to sort it by date too (who first got max records).
I should have a request like it:
select Nickname, Count(*) as Result, MAX(DateAdded) as MDate from Records group by Nickname order by Result Desc, MDate Asc
How to do it by LINQ?
I think this is what you want. I've used extension version of Linq which is probably more easier. The idea is to calculate MaxCount and MaxDate after GroupBy so you can use it in next OrderBy clauses.
db.Records
.Where(i => i.Form.CompetitionID == cID)
.GroupBy(i => i.Nickname)
.Select(g => new { MaxCount = g.Count(), MaxDate = g.Max(i => i.DateAdded), Nickname = g.Key})
.OrderByDescending(gx => gx.MaxCount)
.ThenByDescending(gx => gx.MaxDate)
.Select(gx => new TopUserModel()
{
Nickname = gx.Nickname,
Position = gx.MaxCount
}).Take(100).ToList();
I think what you're asking for is:
...
select new TopUserModel()
{
Nickname = g.Key,
Position = g.Count()
Date = g.Max(r => r.DateAdded)
}).Take(100).OrderByDescending(t => t.Position).ThenBy(t => t.Date).ToList();
When you use group the Key is your grouping but the enumerable is all the records you've grouped so you can still use aggregate functions on them.
If you want to sort by multiple columns, you can put them in order using the chaining above.
var users = (from i in db.Records
where i.Form.CompetitionID == cID
group i by new {i.Nickname} into g
orderby g.Count() descending,
select new TopUserModel()
{
Nickname = g.Key,
Position = g.Count()
Date = g.Max(r => r.DateAdded)
}).Take(100
).OrderBy(c => c.Date).ToList();
Just add max date for nickname to ordering. You also can introduce new range variable for position:
var users = (from r in db.Records
where r.Form.CompetitionID == cID
group r by r.Nickname into g
let position = g.Count()
orderby position descending, g.Max(r => r.DateAdded)
select new TopUserModel {
Nickname = g.Key,
Position = position
}).Take(100).ToList();
Question is already answered here: OrderBy a date field with linq and return ToList()
Add at the end of your users statement
.OrderBy(e => e.Date).ToList();
Related
I have two tables:
PractitionerSkill { Id, Title }
PractitionerInSkills { Id, PractitionerSkillId ), where PractitionerSkillId is FK into PractitionerSkill
(there are more columns but that is not really important)
And I'm trying to count number of skills pr practitioner.
Using LINQ method syntax, I am trying to do this:
SELECT
S.Id, S.Title, COUNT(*) as [Count] from
PractitionerSkills S INNER JOIN
PractitionerInskills PIS ON S.ID = PIS.PractitionerSkillId
GROUP BY
S.Id, S.Title
ORDER BY
S.Title
Easy in SQL. Notice that I'm getting the ID, title and count in the result.
My current efforts (which is not even method syntax)
var query = from skill in _context.PractitionerSkills
join pis in _context.PractitionerInSkills on skill.Id equals pis.PractitionerSkillId into grp
select new
{
Title = skill.Title,
Count = grp.Count()
};
which is almost there, but I can't get more columns out. I need the Skill.Id (or PractitionerInSkills.PractitionerSkillId)
It's easy in Linq too!
var query = _context.PractitionerSkills.Join(_context.PractitionerInSkills,
ps => new { k1 = ps.Id },
pis => new { k1 = pis.PractitionerSkillId },
(ps, pis) => new { ps.Id, ps.Title })
.GroupBy(r => new { r.Id, r.Title })
.Select(g => new { g.Key.Id, g.Key.Title, Count = g.Count() })
.OrderBy(r => r.Title);
I have a SQL Query to extract sorted data by sum of a column value. I have a table named CustomerPoint and It has 2 columns named CusTomerID and Point , I want to extract the person with the highest SUM of points. Here is my SQL Query and it runs properly. But I need to execute it in EF6.3 with LambdaExpressions or Linq queries.
SELECT SUM(Point) AS POINTS, CustomerID FROM CustomerPoint AS POINTS GROUP BY CustomerID ORDER BY POINTS
Thank you for your help!
Something like this:
from p in context.Points
group p by p.CustomerID into gr
select new { CustomerID = gr.Key, POINTS = gr.Sum(c=>c.Point) } into re
orderby re.POINTS
Please try this.
from cp in db.CustomerPoints
Group cp by cp.CusTomerID into cpg
select new { CusTomerID = cpg.Key, Points = cpg.Sum(c => c.Point)}
orderby cpg.Points
And lambda form for diversity:
var query = youContext.CustomerPoints
.GroupBy(x => x.CustomerID)
.Select(x => new { Points = x.Sum(y => y.Point),
CustomerID = x.Key })
.OrderBy(x => x.Points);
I've been stuck with this problem for a few days now.
How do I group by PartCode, take the Max/Min or First value of the Description (items with the same partcode may have a different description), and then sum of the other fields. What I currently have is shown below:
var lengths =
from q in
(from p in partList
select new { p.PartCode, p.Description, p.Quantity, p.TotalMeter, p.PurchaseLength })
group q by new { q.PartCode, q.Description, q.Quantity, q.TotalMeter, q.PurchaseLength } into g
orderby g.Key.PartCode
select new
{
PartCode = g.Max(p => p.PartCode),
Description = g.Max(p => p.Description),
Quantity = g.Sum(p => p.Quantity),
TotalMeter = g.Sum(p => p.TotalMeter),
PurchaseLength = g.Sum(p => p.PurchaseLength)
};
I see two main problems here:
The first is that you say you want to group by PartCode, but instead you're grouping by a combination of everything.
Another possible problem is that you're using Max() on what I can only assume to be collections of strings. This won't fail, but it will select the value that is last in alphabetical order (is that what you want?).
Try this:
var lengths =
from q in
(from p in partList
select new { p.PartCode, p.Description, p.Quantity,
p.TotalMeter, p.PurchaseLength })
group q by q.PartCode into g
orderby g.Key
select new
{
PartCode = g.First().PartCode,
Description = g.First().Description,
Quantity = g.Sum(p => p.Quantity),
TotalMeter = g.Sum(p => p.TotalMeter),
PurchaseLength = g.Sum(p => p.PurchaseLength)
};
If that doesn't solve your issue please tell us what your issue is.
The main objective is to return a list of ingredients in a sorted order according to popularity (which is indicated by how many people have as their favorite a particular ingredient). The SQL equivalent is below:
select distinct COUNT(PersonalInfoes.FavoriteIngredient_ID) as NoUsing,
Ingredients.Name
from PersonalInfoes right join Ingredients
on PersonalInfoes.FavoriteIngredient_ID = Ingredients.ID
group by Ingredients.Name
order by NoUsing desc
What is the equivalent SQL query of method syntax that will produce the same result as above?
The closest I've got is this:
from i in db.Ingredients
join p in db.PersonalInfos on i.ID equals p.FavoriteIngredient.ID into ing
from p in ing.DefaultIfEmpty()
group i by i.Name into g
from p in g
orderby g.Count() descending
select new { Name = g.Key, Count = g.Count() }.Name;
Your query does a left outer join to include ingredients with no favorites. The problem is, for each ingredient with no favorites, you are counting the single value that gets produced by DefaultIfEmpty(). You can exclude the default values when counting like this:
orderby g.Count(x => x != null) descending
You actually don't have to perform a left join. You can just sum the counts of the groups from your group join:
from i in db.Ingredients
join p in db.PersonalInfos on i.ID equals p.FavoriteIngredient.ID into faves
group faves.Count() by i.Name into faveCounts
let count = faveCounts.Sum()
orderby count descending
select new { Name = faveCounts.Key, Count = count };
A subjective problem with your code is it's very confusing with all of the group by and from clauses. If you find this to be an issue, you can try using more descriptive names, using subqueries, or using method syntax.
Edit: Here is a method syntax version. It is not a direct conversion from the query syntax version because that would be quite ugly.
db.Ingredients
.GroupJoin(db.PersonalInfos,
i => i.ID,
p => p.ID,
(i, g) => new { i.Name, FaveCount = g.Count() })
.GroupBy(x => x.Name,
(key, grp) => new { Name = key, Count = grp.Sum(y => y.FaveCount) })
.OrderByDescending(x => x.Count);
Consider a SQL Server table that's used to store events for auditing.
The need is to get only that latest entry for each CustID. We want to get the entire object/row. I am assuming that a GroupBy() will be needed in the query. Here's the query so far:
var custsLastAccess = db.CustAccesses
.Where(c.AccessReason.Length>0)
.GroupBy(c => c.CustID)
// .Select()
.ToList();
// (?) where to put the c.Max(cu=>cu.AccessDate)
Question:
How can I create the query to select the latest(the maximum AccessDate) record/object for each CustID?
I'm wondering if something like:
var custsLastAccess = db.CustAccesses
.Where(c.AccessReason.Length>0)
.GroupBy(c => c.CustID)
.Select(grp => new {
grp.Key,
LastAccess = grp
.OrderByDescending(x => x.AccessDate)
.Select(x => x.AccessDate)
.FirstOrDefault()
}).ToList();
you could also try OrderBy() and Last()
Using LINQ syntax, which I think looks cleaner:
var custsLastAccess = from c in db.CustAccesses
group c by c.CustID into grp
select grp.OrderByDescending(c => c.AccessDate).FirstOrDefault();
Here: this uses max rather than OrderByDesc, so should be more efficient.
var subquery = from c in CustAccesses
group c by c.CustID into g
select new
{
CustID = g.Key,
AccessDate = g.Max(a => a.AccessDate)
};
var query = from c in CustAccesses
join s in subquery
on c.CustID equals s.CustID
where c.AccessDate == s.AccessDate
&& !string.IsNullOrEmpty(c.AccessReason)
select c;