How to properly convert a list to IQueryable on a asp net core web API

Roberto Mejia

im trying to deploy a simple search function that uses a simple tag system, probably there are better ways to bt this is the solution i landed on:

public async Task<ActionResult<IEnumerable<t_usuarios_pub>>> Gett_usuarios_pubByTag(string tag)
        {
            string[] TagList;
            TagList = tag.Split(',');
            List<t_usuarios_pub> results = new List<t_usuarios_pub>();

            var pubs = from m in _context.t_usuarios_pub select m;
            
            if (!String.IsNullOrEmpty(tag))
            {
                foreach (var Itag in TagList)
                {
                    pubs = pubs.Where(s => (s.tag.Contains(Itag) && s.estatus<2)).OrderBy(x => x.estatus);
                    foreach(var x in pubs)
                    {
                        if (!results.Contains(x))
                        {
                            results.Add(x);
                        }
                    }
                }
            }

            return await results.AsQueryable().ToListAsync();
        } 

problem is that the List results cant be converted to IQueryable, this is the error stack. Any idea of how i can properly implement it ?

System.InvalidOperationException: 
The source 'IQueryable' doesn't implement 'IAsyncEnumerable<UserPostAPI.Models.t_usuarios_pub>'.
Only sources that implement 'IAsyncEnumerable' can be used for Entity Framework asynchronous operations.
´´´
jmgregory

Since results is a local variable of List<> type, I don't believe you need to await the last line. It might just work with:

return results.AsQueryable();

In which case, your method may not even need to be an async method. Or, depending on what _context is, perhaps the await needs to be on the pubs filter call:

pubs = await pubs.Where(s => (s.tag.Contains(Itag) && s.estatus<2))
                 .OrderBy(x => x.estatus)
                 .ToListAsync();

Furthermore, since your method says it returns an IEnumerable<>, you probably don't need the .AsQueryable(), either:

return result;

There's also a lot of refactoring you could do -- here are just a few things you may want to consider:

  • Move the String.IsNullOrEmpty(tag) check to the beginning of the method as a guard.
  • If this is user input, trim the split tags in TagList to avoid any issues with extra whitespace, and then be sure to remove any resulting empty members.
  • You could put the estatus < 2 check in your query to reduce having to re-check it again for every item in pubs for every ITag.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to post list of object in ASP.NET Core Web API

ASP.NET convert IQueryable to List

How to properly configure mock dependencies for testing Web Api (ASP.NET Core) controllers using Autofac

How to properly implement "Google Login" in Asp.Net Core Web Api and a spa (Angular)?

How to properly compute Web API methods URLs in TypeScript within ASP.NET Core 1.1. solution?

How to handle multiple endpoints in ASP.Net Core 3 Web API properly

IQueryable, IEnumerable and async in REST Web API for ASP.NET Core and OData

.Net Core web API Paged List - The provider for the source 'IQueryable' doesn't implement 'IAsyncQueryProvider'

C# / ASP.NET Core Web API : how to pass a list parameter?

How to return one of list object in ASP.NET Core 6 Web API?

.net Core 2.0 Web API - Newtonsoft.Json.JsonSerializationException - IQueryable

How to manipulate an IQueryable with Linq in ASP.NET Core

How to authenticate facebook web api in asp.net core 2

How to enable CORS globally in ASP.NET web API core

ASP.NET Core 8 Web API : how to add versioning?

How to Write connection string in asp.net core Web Api?

How does ASP.NET Core Web API build URLs?

how to read posted FormUrlEncodedContent in asp.net core web api?

How to call SQL Queries in ASP .NET Core Web API

How do you Authorize a Web API Controller in ASP Net Core

How to implement search filter in ASP.NET Core Web API

How to post List of data in asp.net web api

ASP.Net web api Vs .Net core web api

Get a list of Roles as a List<string> as result in ASP.NET Core Web API

ASP.Net Core routes with Web API

ASP.NET Core Web API Authentication

debugging the asp.net core web API

Multiple MemoryCache in ASp .Net Core Web API

ASP.NET Core Identity in a Web API