score:3
it is not a direct comparision and isn't on mono, but i have some code which does something similar with a 6mb xml file which i read into a dataset and it has 30,000 rows and takes 0.5 seconds, so i don't think it is the groupby itself that causes the problem.
to diagnose further, i would suggest
testing how long it takes to read the information into a list, ie
var foolist = foostuff.asenumerable().tolist();
testing how long it takes if you change the query to use foolist instead of foostuff
testing how long it takes if you remove footier = g.min(foo => foo.tier) from the select
separate the .field<> reflection from the groupby and time each section, ie first read the information from the datatable into a list , eg
var list2 = (from foo in foostuff.asenumerable() select new { fooid = foo.field<int64>("fooid") tier = foo.field<int>("tier") }).tolist();
then you can query this list
var query = from foo in list2 group foo by foo.fooid into g select new { fooid = g.key, footier = g.min(foo => foo.tier) }; var results = query.tolist();
if this query is slow, it would suggest that there is something wrong with mono's implementation of groupby. you might be able to verify that by using something like this
public static dictionary<tkey, list<tsrc>> testgroupby<tsrc, tkey>
(this ienumerable<tsrc> src, func<tsrc,tkey> groupfunc)
{
var dict= new dictionary<tkey, list<tsrc>>();
foreach (tsrc s in src)
{
tkey key = groupfunc(s);
list<tsrc> list ;
if (!dict.trygetvalue(key, out list))
{
list = new list<tsrc>();
dict.add(key, list);
}
list.add(s);
}
return dict;
}
and to use it
var results = list2.testgroupby(r=>r.fooid)
.select(r=> new { fooid = r.key, footier = r.value.min(r1=>r1.tier)} );
note, this is not meant as a replacement for groupby and does not cope with null keys but should be enough to determine if their is a problem with groupby (assuming mono's implementation of dictionary and list are ok).
score:5
you are materializing all the entities when you call asenumerable(), so your grouping is being done in memory. try removing that part so that the grouping is done at the database level:
var query =
from foo in foostuff
group foo by foo.fooid into g
select new
{
fooid = g.key,
footier = g.min(foo => foo.tier)
};
Source: stackoverflow.com
Related Query
- LINQ GroupBy extremely slow
- LINQ Source Code Available
- LINQ Lambda efficiency of code groupby orderby
- creating Linq to sqlite dbml from DbLinq source code
- source code for LINQ 101 samples
- LINQ GroupBy Source = A Destination = B or Source = B and Destination = A
- Linq Code with Distinct or GroupBy not working
- Optimise and speed up very slow Linq / SQL code
- c# Linq or code to extract groups from a single list of source data
- Linq To Entities query is extremely slow
- LINQ Search nvarchar(MAX) column extremely slow using .Contains()
- Convert string[] to int[] in one line of code using LINQ
- LINQ with groupby and count
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- Linq to Objects: does GroupBy preserve order of elements?
- Linq code to select one item
- Entity-framework code is slow when using Include() many times
- How are people unit testing code that uses Linq to SQL
- C# Groupby Linq and foreach
- GroupBy with linq method syntax (not query syntax)
- LINQ query to perform a projection, skipping or wrapping exceptions where source throws on IEnumerable.GetNext()
- How to select top N rows in a Linq GroupBy
- How to select last record in a LINQ GroupBy clause
- Translate SQL to lambda LINQ with GroupBy and Average
- Key comparisons for Linq GroupBy using Default EqualityComparer
- LINQ - GroupBy a key and then put each grouped item into separate 'buckets'
- Syntax to execute code block inside Linq query?
- C#, EF & LINQ : slow at inserting large (7Mb) records into SQL Server
- How to use GroupBy using Dynamic LINQ
- Slow foreach() on a LINQ query - ToList() boosts performance immensely - why is this?
More Query from same tag
- How does a LINQ expression know that Where() comes before Select()?
- How to remove from one list all the items in another?
- Linq Query modification to include a value in a related table
- Convert object to a queryable
- ExceptionLogger with using xml
- LINQ to SQL: intermittent AccessViolationException wrapped in TargetInvocationException
- Date comparision using Linq
- C# Not Rounding Double
- Convert this LINQ to dynamic Expression tree
- LINQ: Group By, and retrieve common property
- LINQ query working fine in Dot Net Core 2.2 but in 3.1
- Linq GroupBy Max and add column
- How can i filter my data by foreign key before lucene searching?
- Using join on same table three times
- How to Parse a nullable DateTime during a LINQ group operation?
- Linq to Entities group by week number
- How can i get a single response using linq
- Creating a list of objects belonging to another class using linq
- MS SQL distinct results based on other table
- How do i obtain foreign table field using string value for fieldname with reflection and entity framework
- Possible uses for Intersect
- Speeding up my simple Redis .NET application
- LINQ - How to write a query, when the model has a list property?
- How to sort a dictionary by key
- How can i get my ListBox working correctly
- Swap two items in List<T>
- Can not get the expected result in many to many relationship using LINQ in Asp.net C#, MVC, Codefirst application
- Linq Query is taking long time
- LINQ: adding OrderBy to query
- How do I select multiple grandchildren in linq?