System.Linq.Async | Prototype of LINQ over IAsyncEnumerable T using C | Database library
kandi X-RAY | System.Linq.Async Summary
kandi X-RAY | System.Linq.Async Summary
Prototype of LINQ over IAsyncEnumerable using C# 8.0 syntax for foreach await and async iterators.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of System.Linq.Async
System.Linq.Async Key Features
System.Linq.Async Examples and Code Snippets
Community Discussions
Trending Discussions on System.Linq.Async
QUESTION
I have a few AsyncEnumerable
s that I would like to merge in a single AsyncEnumerable
, which should contain all the elements that are emitted concurrently from those sequences. So I used the Merge
operator from the System.Interactive.Async package. The problem is that this operator does not always treat all sequences as equal. In some circumstances it prefers emitting elements from the sequences that are on the left side of the arguments list, and neglects the sequences that are on the right side in the arguments list. Here is a minimal example that reproduces this undesirable behavior:
ANSWER
Answered 2022-Jan-16 at 01:25Here is the final code. The algorithm has been modified to suit the OP. I have left the original code below.
This use a greedy algorithm: the first available value is returned, and no attempt is made to merge in turn. Each time a task finishes, the next one for the same enumerator goes to the back, ensuring fairness.
The algorithm is as follows:
- The function accepts a
params
array of sources. - Early bail-out if no source enumerables are provided.
- Create a list to hold the enumerators along with their respective tasks as tuples.
- Get each enumerator, call
MoveNextAsync
and store the pair in the list. - In a loop, call
Task.WhenAny
on the whole list. - Take the resulting
Task
and find its location in the list. - Hold the tuple in a variable and remove it from the list.
- If it returned
true
, thenyield
the value and callMoveNextAsync
again for the matching enumerator, pushing the resulting tuple to the back of the list. - If it returns
false
, thenDispose
the enumerator. - Continue looping until the list is empty.
finally
block disposes any remaining enumerators.- There is also an overload to provide a cancellation token
There are some efficiencies to be had in terms of allocations etc. I've left that as an exercise to the reader.
QUESTION
I have two asynchronous sequences that I want to "zip" in pairs, and for this purpose I used the Zip
operator from the System.Linq.Async package. This operator behaves in an undesirable way though, at least for my case. Instead of enumerating the two sequences concurrently, it enumerates them sequentially, with the result being that the latency is added up. Each of my sequences emits an element every one second on average, and I expected that the combined sequence would also emit zipped pairs every one second, but in reality I get one pair every 2 seconds. Below is a minimal example that demonstrates this behavior:
ANSWER
Answered 2022-Jan-06 at 08:45Something like this appears to work:
QUESTION
Is it possible? How?
I have an asynchronous method fetching rows of data from my database.
My result is IAsyncEnumerable
.
It should be a perfect data type for a fast and responsive UI, however - when I set it as a datasource for my grid it just doesn't work and shows nothing.
I have 2 workarounds for this. First is obviously to return array from my async task. The second one is similar - I can just use ToArrayAsync()
on result (from System.Linq.Async
).
I suspect that using ToArrayAsync()
is slower than just returning the array from the task, so it doesn't make much sense.
It would make a lot of sense if IAsyncEnumerable
type could be used directly as a data source for the control. But then I probably need to implement some kind of IObservable
interface or something like that. Did anyone tried this?
The whole thing is pretty new and I can't find any examples, or even an article stating that it's even possible. I mean - what good IAsyncEnumerable
is if you cannot use it for the UI?
Update: what I'm looking for is a conversion between IAsyncEnumerable
and ObservableCollection
. I found examples, but they all work exactly as my mentioned workaround - so the data is put into an array and then added to the collection. This defeats all IAsyncEnumerable
benefits.
ANSWER
Answered 2021-Sep-26 at 00:27IAsyncEnumerable
is introduced to allow async data generators/data streams. Usually controls like ItemsControl
that display a collection of data items are index based. And ICollectionView
based.
Usually the control gets the items via the ICollectionView
of the original collection. A data item, identified by its index in the source collection's ICollectionView
, is wrapped into an UIElement
container for rendering.
A data stream/generator is not index based. An Enumerator
(result of a call to IEnumerable.GetEnumerator
) is used to step from element to element by invoking IEnumerator.MoveNext
(foreach
is a language construct that for convenience encapsulates the actual enumeration logic that involves the IEnumerator
).
Aside from potential threading problems introduced by an async data stream, the index based ItemsControl
needs to know all it's items in advance - that's why you need a collection that implements INotifyCollectionChanged
in order to have the ItemsControl
update its Items
on changes: the View is considered to be passive - it only displays data and does not generate the data.
Although not mandatory, WPF was designed with MVVM in mind. The active part is usually the Model or the View Model. Data generation would usually take place in the Model. So, it does not make sense to have IAsyncEnumerable
as direct binding source as this would mean that the passive UI element must generate/consume the data stream directly. You would want this to be a responsibility of the View Model/Binding.Source
. And again, the UI is designed to work on ICollectionView
implementations and not on collections and their iterators directly.
What you can do is to enumerate the IAsyncEnumerable
in the Binding.Source
and update an ObservableCollection
with the generated results:
QUESTION
I am getting the following error when using .NET Core 2.2 and SQL Always Encrypted. Is this supported in 2.2?
Keyword not supported: 'column encryption setting'.
Actually, I run DB migration in my AppContext constructor as you can see there.
...ANSWER
Answered 2021-Jun-03 at 10:38Need to add the following package reference Microsoft.Data.SqlClient (see nuget) and use this Microsoft.Data.SqlClient instead of System.Data.SqlClient.
It is supported in .NET Core 3.0+ versions
QUESTION
I have an asynchronous sequence (stream) of messages that are arriving sometimes numerously and sometimes sporadically, and I would like to process them in batches of 10 messages per batch. I also want to enforce an upper limit to the latency between receiving a message and processing it, so a batch with fewer than 10 messages should also be processed, if 5 seconds have passed after receiving the first message of the batch. I found that I can solve the first part of the problem by using the Buffer
operator from the System.Interactive.Async package:
ANSWER
Answered 2021-May-23 at 20:10UPDATE: Changed the solution to try and accommodate OP's notes and requests. This method utilizes the wonderful Nito.AsyncEx package, which was not explicitly named as allowed by OP.
QUESTION
I am using the latest version of .NET Core (.NET 5) and Entity Framework Core 6 (preview) to connect to a MySQL database. I am trying to use GroupBy to generate a group by query to execute on the DB server, as described here. Unfortunately, this fails to compile with the error
The call is ambiguous between the following methods or properties: 'System.Linq.Queryable.GroupBy(System.Linq.IQueryable, System.Linq.Expressions.Expression>)' and 'System.Linq.AsyncEnumerable.GroupBy(System.Collections.Generic.IAsyncEnumerable, System.Func)
This error is related to LINQ and EF Core sharing the same methods, and is discussed in detail here. I have tried the suggested workaround of creating extension methods for each LINQ call, with the following code:
...ANSWER
Answered 2021-Mar-11 at 20:49Use .AsQueryable()
on your DbSet
when IAsyncEnumerable
is not needed and visa versa to remove ambiguity:
QUESTION
I have an IAsyncEnumerable
stream that contains data downloaded from the web, and I want to save asynchronously each piece of data in a SQL database. So I used the ForEachAwaitAsync
extension method from the System.Linq.Async library. My problem is that downloading and saving each piece of data is happening sequentially, while I would prefer if it happened concurrently.
To clarify, I don't want to download more than one pieces of data at the same time, neither I want to save more than one pieces of data at the same time. What I want is that while I am saving a piece of data in the database, the next piece of data should be concurrently downloaded from the web.
Below is a minimal (contrived) example of my current solution. Five items are downloaded and then are saved in the database. Downloading each item takes 1 second, and saving it takes another 1 second:
...ANSWER
Answered 2021-Feb-15 at 21:55It sounds like you just need to keep track of the previous action's Task and await it before the next action Task.
QUESTION
I can get objects from EF with request like this:
...ANSWER
Answered 2020-Dec-23 at 19:26Was not able to get the same error as you (got just failed translation one, so it would be great if you could add minimal reproducible example), for me worked using EF.Functions.ILike
(with latest npgsql package):
QUESTION
I've watched the chat on LINQ with IAsyncEnumerable which has given me some insight on dealing with extension methods for IAsyncEnumerables, but wasn't detailed enough frankly for a real-world application, especially for my experience level, and I understand that samples/documentation don't really exist as of yet for IAsyncEnumerable
s
I'm trying to read from a file, do some transformation on the stream, returning a IAsyncEnumerable
, and then send those objects downstream after an arbitrary number of objects have been obtained, like:
ANSWER
Answered 2020-Jul-29 at 12:17There is an operator Buffer
that does what you want, in the package System.Interactive.Async.
QUESTION
I've tried to define a gRPC service where client can subscribe to receive broadcasted messages and they can also send them.
...ANSWER
Answered 2020-Jun-20 at 14:18The problem you're experiencing is due to the fact that MessengerServer.SubscribeForMessages
returns immediately. Once that method returns, the stream is closed.
You'll need an implementation similar to this to keep the stream alive:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install System.Linq.Async
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page