Linq GroupBy and multiple columns
Tag : chash , By : esimran
Date : March 29 2020, 07:55 AM
Hope that helps In your particular case, you could use x.First().Team inside your Select to get your team information. For other cases of actually needing to group on multiple fields, you could group on an anonymous type. Such as
|
How to .GroupBy multiple columns LINQ/Projection?
Tag : linq , By : Gipsy.D
Date : March 29 2020, 07:55 AM
I wish did fix the issue. How can I group by multiple columns using linq projection? , You can GroupBy an anonymous type: var q = db.Areas.GroupBy(
x => new
{
CatId = x.AreaCatId,
CatName = x.AreaCatName,
Id = x.AreaId,
Name = x.AreaName
});
|
LINQ GroupBy multiple columns and Count
Date : March 29 2020, 07:55 AM
I hope this helps you . As far as I have understood, you want to display the JobsCount & Days Count for each person and each having different set of titles, in that case try this:- var result = tstPData.GroupBy(x => new { x.PersonnelId, x.Name , x.Position })
.Select(x => new
{
PersonnelId = x.Key.PersonnelId,
Name = x.Key.Name,
Position = x.Key.Position ,
JobCount = x.Count(),
DaysCount = x.Count()
});
List<PersonnelStats> persons = new List<PersonnelStats>
{
//Same Job for Alex but different date
new PersonnelStats { Name="Alex", Position="Dev", JobId= new Guid("11223344-5566-7788-99AA-BBCCDDEEFF00"), PersonnelId = new Guid("11223344-5566-7788-99AA-BBCCDDEEFF00"), Date= new DateTime(2015,03,12)},
new PersonnelStats { Name="Alex", Position="Dev", JobId= new Guid("11223344-5566-7788-99AA-BBCCDDEEFF00"), PersonnelId = new Guid("11223344-5566-7788-99AA-BBCCDDEEFF00"), Date= new DateTime(2015,03,15)},
//Two jobs for Mary
new PersonnelStats { Name="Mary", Position="App", JobId= new Guid("11229944-2356-7788-99AA-BBCCDDEEFF00"), PersonnelId = new Guid("11223344-1567-6565-99AA-BBCCDDEEFF00"), Date= new DateTime(2015,03,13)},
new PersonnelStats { Name="Mary", Position="App", JobId= new Guid("11229944-3445-7788-99AA-BBCCDDEEFF00"), PersonnelId = new Guid("11223344-1567-6565-99AA-BBCCDDEEFF00"), Date= new DateTime(2015,03,13)},
//Two jobs for Mark with 1 job being at different dates.
new PersonnelStats { Name="Mark", Position="Test", JobId= new Guid("11223344-7879-2344-99AA-BBCCDDEEFF00"), PersonnelId = new Guid("11223344-2245-4343-99AA-BBCCDDEEFF00"), Date= new DateTime(2015,03,17)},
new PersonnelStats { Name="Mark", Position="Test", JobId= new Guid("11223344-5522-2344-99AA-BBCCDDEEFF00"), PersonnelId = new Guid("11223344-2245-4343-99AA-BBCCDDEEFF00"), Date= new DateTime(2015,03,17)},
new PersonnelStats { Name="Mark", Position="Test", JobId= new Guid("11223344-5522-2344-99AA-BBCCDDEEFF00"), PersonnelId = new Guid("11223344-2245-4343-99AA-BBCCDDEEFF00"), Date= new DateTime(2015,03,18)},
};
var result = persons.GroupBy(x => new { x.PersonnelId, x.Name, x.Position })
.Select(x => new
{
PersonnelId = x.Key.PersonnelId,
Name = x.Key.Name,
Position = x.Key.Position,
JobCount = x.Select(z => z.JobId).Distinct().Count(),
DaysCount = x.Select(z => z.Date.Date).Distinct().Count()
});
|
Linq Min in Select new & Multiple GroupBy columns
Tag : chash , By : Kristian Hofslaeter
Date : March 29 2020, 07:55 AM
I hope this helps you . I want to write following query in Linq , Try something like this: var data = ctx.tblInOut
.Where(m =>
m.CompanyId == companyId &&
m.Time_Field1 == item.ShiftCode &&
m.InDate == StrInStart &&
m.InDate <= StrInEnd &&
m.Time_Date1 == InputDate
)
.GroupBy(m =>
new {
m.Code,
m.Time_Date1,
m.Time_FLD1,
m.Time_FLD2
})
.Select(g =>
new
{
m.Key.Code,
InDate = m.Min(gg => gg.InDate),
m.Key.Time_Date1,
Something = "I",
m.Key.Time_FLD1,
m.Key.Time_FLD2,
SomeDate = "31/05/2015"
}).ToList();
|
linq.js GroupBy on multiple columns with Sum
Tag : chash , By : user161380
Date : March 29 2020, 07:55 AM
I wish this help you When grouping by composite keys, you need to provide a compare selector which converts the keys into a representation that can be compared (usually strings). Try this instead: var query = Enumerable.From(data)
.Where("$.GlPartnerLevel2 != null")
.GroupBy(
"{ PL1: $.GlPartnerLevel1 , PL2: $.GlPartnerLevel2 }",
"$.Value | 0",
"{ Name: $.PL2, ParentName: $.PL1, Value: $$.Sum() }",
"$.PL1 + ' ' + $.PL2") // this must be included
.ToArray();
|