LINQ query to group results into an array
Tag : chash , By : user105769
Date : March 29 2020, 07:55 AM
will be helpful for those in need Given: , This looks straightforward, unless I missed your meaning: IEnumerable<GroupItem> linqQuery = detailList
.GroupBy(i => i.A)
.Select(g => new GroupItem()
{
A = g.Key,
AllBsWithinA = g.Select(i => i.B).ToList()
});
|
Linq to Entities group query giving list of results in each group
Date : March 29 2020, 07:55 AM
it should still fix some issue If you want to get List of Ids for each group then you have to select x.Select(r => r.Id) like: var result = Items.GroupBy(x => new { x.Size, x.Type })
.Select(x => new
{
Key = x.Key,
Ids = x.Select(r => r.Id)
});
|
Linq group query gives wrong results
Tag : chash , By : amorican
Date : March 29 2020, 07:55 AM
will be helpful for those in need In order for GroupBy to produce a grouping, the object by which you group must have an override of GetHashCode and Equals, or you need to provide a suitable equality comparer to GroupBy. Your Stock does not override GetHashCode/Equals, and your GroupBy query does not use a custom equality comparer, so you get unexpected results. public class Stock {
public string Name {get; set;}
public List<int> Values {get; set;}
public float Price {get; set;}
public override bool Equals(object obj) {
if (obj == this) return true;
var other = obj as Stock;
if (other == null) return false;
return Name.Equals(other.Name)
&& Price == other.Price
&& Values.SequenceEqual(other.Values);
}
public override int GetHashCode() {
return Name.GetHashCode()
+ Price.GetHashCode()
+ Values.Sum();
}
}
|
LINQ merge 2 query results
Tag : chash , By : Chris Woods
Date : March 29 2020, 07:55 AM
To fix this issue The datatable has 5 columns , This works perfectly well for your code., DataRow[] dr1 = dtt.Select("Course = 'Math' AND Month > 10");
var result_one = dr1.AsEnumerable()
.GroupBy(r => new { Name = r.Field<string>("Name"), Class = r.Field<string>("Class") })
.Select(g => new
{
Name = g.Key.Name,
Class = g.Key.Class,
Max = g.Max(r => r.Field<int>("Score")),
Max_Month = g.FirstOrDefault(gg => gg.Field<int>("Score") == g.Max(r => r.Field<int>("Score"))).Field<int>("Month"),
}
).Distinct().ToList();
DataRow[] dr2 = dtt.Select("Course = 'Chem'");
var result_two = dr2.AsEnumerable()
.GroupBy(r => new { Name = r.Field<string>("Name"), Class = r.Field<string>("Class") })
.Select(g => new
{
Name = g.Key.Name,
Class = g.Key.Class,
Chem_Max = g.Max(r => r.Field<int>("Score")),
Chem_Max_Month = g.FirstOrDefault(gg => gg.Field<int>("Score") == g.Max(r => r.Field<int>("Score"))).Field<int>("Month"),
}
).Distinct().ToList();
var lstLeftJoin = (from a in result_one
join b in result_two
on new { a.Name, a.Class } equals new { b.Name, b.Class }
into gj
from subpet in gj.DefaultIfEmpty()
select new { a.Name, a.Class, Math_Max_Month = a.Max_Month, Math_Max = a.Max, Chem_Max_Month = (subpet == null ? 0 : subpet.Chem_Max_Month), Chem_Max = (subpet == null ? 0 : subpet.Chem_Max) }).ToList();
var lstRightJoin = (from a in result_two
join b in result_one
on new { a.Name, a.Class } equals new { b.Name, b.Class }
into gj
from subpet in gj.DefaultIfEmpty()
select new { a.Name, a.Class, Math_Max_Month = (subpet == null ? 0 : subpet.Max_Month), Math_Max = (subpet == null ? 0 : subpet.Max), a.Chem_Max_Month, a.Chem_Max }).ToList();
var lstUnion = lstLeftJoin.Select(s => new { Name = s.Name, Class = s.Class, Math_Max_Month = s.Math_Max_Month, Math_Max = s.Math_Max, Chem_Max_Month = s.Chem_Max_Month, Chem_Max = s.Chem_Max }).Union(lstRightJoin.Select(s => new { Name = s.Name, Class = s.Class, Math_Max_Month = s.Math_Max_Month, Math_Max = s.Math_Max, Chem_Max_Month = s.Chem_Max_Month, Chem_Max = s.Chem_Max })).OrderBy(o => o.Name).ThenBy(c => c.Class).ToList();
Name Class Math_Max_Month Math_Max Chem_Max_Month Chem_Max
Alex C1 12 90 null null
Alex C2 11 91 12 92
Bob C1 null null 12 97
Bob C2 12 94 null null
|
How to group DataTable results using Linq query expression
Tag : chash , By : kameel
Date : March 29 2020, 07:55 AM
this will help I have the following DataTable , You need insert String.Join in your code: var BooksList = from Book in BookTable.AsEnumerable()
group Book by Book.Field<string>("BookCategory") into g
let list = g.ToList()
select new
{
BookCategory = g.Key,
BookNames = string.Join(" ; ",list.Select(x => x.Field<string>("BookName").ToString()))
};
|