Passing LINQ Query When Using a Repository Pattern

Brian Ashcraft 100 Reputation points
2023-02-26T01:38:22.09+00:00

Hello,

I have a C# ASP N-Tier .Net 6 Razor (non-MVC) web application that is using a standard Repository Pattern and Unit of Work.

Partial view below:

public IEnumerable<T> GetAll()
{
    IQueryable<T> query = dbSet;
    return query.ToList();
}

public IEnumerable<T> Find(Expression<Func<T, bool>> expression)
{
    return _db.Set<T>().Where(expression);
}

In my index.cshtml page I have a small form that allow a user to Search/Filter the data displayed on the page:

2023-02-25_19-06-18

The input from that search box is captured in the following Property:

[BindProperty(SupportsGet = true)]
public string SearchString { get; set; }

If SearchString has a value I want to return a subset of the data, based on the user's input.

If it does not have a value I GetAll().

public void OnGet()
{
    if (!string.IsNullOrEmpty(SearchString))
    {
        var attributionList = from attribution in Attribution
                              where attribution.Description
                              .Contains(SearchString)
                              select attribution;

        Attribution = _UnitOfWork.Attribution.GetAll().Select(attributionList);
    }
    else
    {
        Attribution = _UnitOfWork.Attribution.GetAll();
    }
}

I am receiving the following error on this part of the code:

.Select(attributionList)

Error CS0411

The type arguments for method 'Enumerable.Select<TSource, TResult>(IEnumerable<TSource>, Func<TSource, int, TResult>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

Any help in resolving this is greatly appreciated.

Thanks for your time!

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
740 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,540 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,470 questions
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,733 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 116.8K Reputation points
    2023-02-26T19:36:08.73+00:00

    Check this:

    Attribution = _UnitOfWork.Attribution.Find( a => a.Description.Contains( SearchString ) ).ToList();
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.