[Question] Remote Linq sync to async
See original GitHub issueGot this synchronous code working perfectly in a Blazor server app:
public static IQueryable<T> Create<T>(IClientSession session, ListType? mainListType = null, int? subTypeID = null)
{
return RemoteQueryable.Factory.CreateQueryable<T>(
expression =>
{
return session.Execute(new GetExpressionResult(expression, typeof(T), mainListType, subTypeID))?.Result ?? new List<T>();
},
null,
new BasicResultMapper(),
null);
}
I’m calling this sync code similar to this:
Helper.Create<MyDto>(ClientSession).Where(x => x.ForType == ItemType.Orders).OrderBy(x => x.Name).Take(25).ToList();
However can’t get this working async using Remote.Linq.
I can have it working by using Task, but that doesn’t feel 100% correct:
public static IQueryable<T> Create<T>(IClientSession session, ListType? mainListType = null, int? subTypeID = null)
{
return RemoteQueryable.Factory.CreateQueryable<T>(
expression =>
{
return session.Execute(new GetExpressionResult(expression, typeof(T), mainListType, subTypeID))?.Result ?? new List<T>();
},
null,
new BasicResultMapper(),
null);
}
public static async Task<IQueryable<T>> CreateAsync<T>(IClientSession session, ListType? mainListType = null, int? subTypeID = null)
{
return await Task.Run(() =>
{
return Create<T>(session, mainListType, subTypeID);
});
}
Another attempt, but this deadlocks or doesn’t seem to work property in production:
public static async Task<IQueryable<T>> CreateAsync<T>(IClientSession session, ListType? mainListType = null, int? subTypeID = null)
{
return RemoteQueryable.Factory.CreateAsyncQueryable<T>(
async expression =>
{
return (await session.ExecuteAsync(new GetExpressionResult(expression, typeof(T), mainListType, subTypeID)))?.Result ?? new List<T>();
},
null,
new BasicResultMapper(),
null);
}
Another attempt, but one that doesn’t build (complains about return type should be void, Task, Task<T>, IAsyncEnumerable<T>, … CS1983 and lacks await CS1998):
public static async System.Linq.IAsyncQueryable<T> CreateAsync<T>(IClientSession session, ListType? mainListType = null, int? subTypeID = null)
{
return RemoteQueryable.Factory.CreateAsyncQueryable<T>(
async expression =>
{
return (await session.ExecuteAsync(new GetExpressionResult(expression, typeof(T), mainListType, subTypeID)))?.Result ?? new List<T>();
},
null,
new BasicResultMapper(),
null);
}
How can this sync method be written correctly async using your library?
Issue Analytics
- State:
- Created a year ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Changing a IQueryable synchronous method to async
i found out how to do that , the problem was i had to change IQueryable to IList. ... Models.Campaign>> WhereAsync(System.Linq.Expressions.
Read more >Feature request for asynchronous data provider support. #45
I tried to use Remote.Linq client side in an asynchronous manner. I might be doing it wrong, if not then please add support...
Read more >Any benefit of async/await in rest api with synchronous flow?
To me the async advantage would help when the client is doing async calls using WebAPI where the controller is doing async CRUD...
Read more >To Async or not to Async? : r/csharp
I'm in a discussion with my team about the use of async/await in our project. We're writing a small WebAPI. Nothing fancy.
Read more >Mixing Async and Sync in same HTTP request ASP.NET ...
Is it bad practice to mix async and sync call in same ASP.NET core API call? For example, in following code method CropBlackBroderOfAnImageAsync...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Synchronous linq functions should actually work too. Please note that by adding namespace
using Remote.Linq.Async;
you get a set of async linq functions as well.Have you already checked-out the sample/demo apps (Remote.Linq.Samples.sln)? There are a few showing how to work async.
This issue is closed due to inactivity. Feel free to re-open if needed.