Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
293 views
in Technique[技术] by (71.8m points)

c# - Entity Framework Core in ASP.NET 5 web project - Autogenerate LINQ filters query

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?

question from:https://stackoverflow.com/questions/65952916/entity-framework-core-in-asp-net-5-web-project-autogenerate-linq-filters-query

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...