6

I have .NET 5 MVC project (original ASP.NET 3.1 Core MVC), where I use EF Core 5 + React + Redux and MSSql.

I looking for a find way, how to send via REST GET request, data from React form (the form with, select, input, range boxes) into the backend and autogenerate WHERE SQL query from the sand filters, with selected values.

Is there any EF Core feature or I must generate this query for example in FOREACH like:

query = ...
foreach param_filter in
   query = query.Where(...)

I have an HTML form with filters and their values are generated from DB, and each website page has another count and types of filters, but each filter has a unique string ID

ADD INFO:

Input request:

{
    "name": "Application",
    "values": [
        {
            "name": "Smart Homes & Cities | Industrial | Automotive | Security | Consumer",
            "value": "Smart Homes & Cities | Industrial | Automotive | Security | Consumer"
        }
    ],
    "type": "select",
    "field": "ant_application",
    "sort": 4
}

I would like to get REST API request with filters array, and for each field type, generate something like:

foreach (var f in filters)
    query.Where(p => p.Field == p.field).AndValueNotIn(f.values);

Where p means products list, where each product has its own field values

Now I use in EF Core DB procedure call like:

.FromSqlInterpolated($"EXEC GetGroupFilters {groupId}").AsEnumerable()

MSSql procedure has the only SQL (join, select, union), and returns formatted rows from DB which I load into Object and use LINQ, I have 2 choices:

  1. generate raw text from filters with SQL WHERE and send it into the procedure
  2. generate LINQ .Where() etc in backend side with C#

I think then choice number 1 will be faster and better, but still, find the best solution

Is there any better solution?

8
  • 1
    I recommend to look here Commented Jan 31, 2021 at 20:40
  • 1
    This is really broad. Provides and specifications for the types and meanings of inputs. Commented Feb 2, 2021 at 17:54
  • 1
    @AluanHaddad I add more info to the original question. Commented Feb 2, 2021 at 18:06
  • 2
    You can simply chain Wheres: query.Where(...).Where(...).Where(...). There are many questions on how to compose filter queries at runtime. Commented Feb 2, 2021 at 20:38
  • 1
    Chaining Where statements is what you want. It's simple. You can build the predicates using a predicate builder. This is all stuff that has been covered many times before. Commented Feb 3, 2021 at 8:10

1 Answer 1

1

In fact, I have done such a thing

I Recommend You to Learn about Expression Trees in Linq.Expression as The Link Below I Implement this Before You can Find Your Questions Answers in this link

but for More Understading How can Implement This I Put Some Example Codes Below:

     // Comment   _Queryable.Where(x=>x.Something) | This Queryable from 
     // Type Of Generics (Where Any model With Any Field With This)
     var query = Expression.Equal(Expression.Property(keyParameter, filter.Key),
                        Expression.Constant(filter.Value));
                    MethodCallExpression whereExpression = Expression.Call(
                        typeof(Queryable),
                        "Where",
                        new Type[] {_queryable.ElementType},
                        _queryable.Expression,
                        Expression.Lambda<Func<T, bool>>(query, new 
                      ParameterExpression[] {keyParameter}));
                    _queryable = 
          _queryable.Provider.CreateQuery<T(whereExpression);

Sorry For My Bad English I Wish Its Help You

Sign up to request clarification or add additional context in comments.

1 Comment

This sample code is not exactly what you want but it has the concepts you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.