Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grouped query does not return all rows #11861

Closed
sam-wheat opened this issue Apr 30, 2018 · 11 comments
Closed

Grouped query does not return all rows #11861

sam-wheat opened this issue Apr 30, 2018 · 11 comments
Labels
closed-no-further-action The issue is closed and no further action is planned.

Comments

@sam-wheat
Copy link

Query below returns 45282 symbols:

List<string> symbols = await db.DTOObservations.GroupBy(x => x.Symbol).Select(x => x.Key).ToListAsync();
 

foreach(string symbol in symbols)
{
	await func(symbol);
};

Query below returns 10616 symbols:

var query = db.DTOObservations.GroupBy(x => x.Symbol);  

await query.ForEachAsync(async symbols =>
{
	await func(symbols.Key);
});

Am I doing something obviously wrong? Is this a known problem for version 2.0.2? I will try to reproduce it in an example project if not.

@smitpatel
Copy link
Member

Afaik, there is no known problem like that. It is hard to figure out what do you mean by return where you are just calling a func over the grouping key.
Given that it is different results, please provide a stand-alone repro with seeding code which demonstrate the issue you are seeing.

@sam-wheat
Copy link
Author

what do you mean by return

In the first example the List named "symbols" is populated with 45282 strings.

In the second example the method named "ForEachAsync" is called 10616 times (should be 45282).

@smitpatel
Copy link
Member

I am not able to see difference in data for my sample test. Can you provide a repro?

@sam-wheat
Copy link
Author

will do asap, am two weeks behind on my project at the moment.

@sam-wheat
Copy link
Author

Please see attached project. Tests fail for me 100% of the time using both MSSQL and MySQL. I'm not sure of the significance of adding dupe rows. I just did that to simulate my real project.

ForEachAsyncTest.zip

@smitpatel
Copy link
Member

@sam-wheat - The issue is the lambda passed to ForEachAsync. While it compiles correctly, it takes arg of Action<T> which is not async so in the implementation it is not awaited. But in your case you are using async lambda which does await Task.Delay(1). So if the task did not finish immediately, it is forgotten. Hence records are lost.

Is there a reason you need to pass in async lambda to ForEachAsync method?

@sam-wheat
Copy link
Author

@smitpatel Yes I need to await an async method call in that block.

@smitpatel
Copy link
Member

@sam-wheat - Let me rephrase is slightly. Do you need async lambda only. Or given that you can only pass sync lambda, you can call async method inside lambda like the way you call async from sync.

@sam-wheat
Copy link
Author

My actual code is in the first post in this thread. I don't think I should call async code from sync block, it would be easier to just use a foreach loop on the query. I suggest Func<T,Task> as an argument for future versions of ForEachAsync.

@anpete
Copy link
Contributor

anpete commented May 1, 2018

@sam-wheat You can workaround this by hand-coding the foreach loop, or creating your own version of ForEachAsync. You can use AsAsyncEnumerable to get an IAsyncEnumerable.

@sam-wheat
Copy link
Author

I think that is the best choice for the immediate problem.
Thanks very much.

@smitpatel smitpatel added the closed-no-further-action The issue is closed and no further action is planned. label May 1, 2018
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-no-further-action The issue is closed and no further action is planned.
Projects
None yet
Development

No branches or pull requests

4 participants