question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] Remote Linq sync to async

See original GitHub issue

Got 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:closed
  • Created a year ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
6beecommented, May 17, 2022

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.

0reactions
6beecommented, Jun 22, 2022

This issue is closed due to inactivity. Feel free to re-open if needed.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found