Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to create Advanced query Service by ynamic LINQ

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to create an advanced query service for ynamic LINQ. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

Foreword:

In business development, a commonly used feature is "advanced query", in which customers can set query conditions to find data according to their needs, similar to the following figure:

In general, we need to customize the Dto class for each "advanced query" to transfer conditions, and to combine the conditions into query statements to perform database query operations, which is time-consuming and laborious.

Now, you can easily do this with AutoFilterer.Generators.

First, create an advanced query service

For example:

Create an Asp.Net Core Web API project that references the Nuget package AutoFilterer.Generators.

Add GenerateAutoFilterAttribute to the WeatherForecast class:

[GenerateAutoFilter] public class WeatherForecast {public DateTime Date {get; set;} public int TemperatureC {get; set;} public int TemperatureF = > 32 + (int) (TemperatureC / 0.5556); public string Summary {get; set;}

Then, modify the Get method of WeatherForecastController.cs to add the WeatherForecastFilter parameter:

[HttpGet]:

Public IEnumerable Get ([FromQuery] WeatherForecastFilter filter) {var rng = new Random (); / / Change range to 100 from 5 to get more reasonable results. Return Enumerable.Range (1,100) .Select (index = > new WeatherForecast {Date = DateTime.Now.AddDays (index), TemperatureC = rng.Next (- 20,55), Summary = Summaries [rng.Next (Summaries.Length)]}) .AsQueryable (). ApplyFilter (filter) / / use filter .ToArray ();}

AutoFilterer.Generators provides an IQueryable.ApplyFilter (filter) extension method to filter data based on advanced query criteria.

To run the program, you can see the following figure in Swagger UI:

The Advanced query service is now complete. You can pass in the range of minimum and maximum values, sorting, and paging.

Conclusion:

AutoFilterer.Generators provides a convenient way to implement "advanced query". If you have similar business requirements, you can give it a try.

However, AutoFilterer.Generators can only provide simple range filtering:

Today, we show you how to easily implement more powerful advanced query services using Dynamic LINQ.

Use Dynamic LINQ to create advanced query services

For example:

Create an ASP.NET Core Web API project that references the Nuget package System.Linq.Dynamic.Core.

And add to the WeatherForecastController.cs header:

Using System.Linq.Dynamic.Core;1. Define query parameters

Create a DynamicLinqDto, which is used to pass returned fields, query criteria, sorting method, paging method, and so on:

Public class DynamicLinqDto {public string Fields {get; set;} public string Filter {get; set;} public string OrderBy {get; set;} public int? PageNo {get; set;} public int? PageSize {get; set;}} 2. Implement the query method

Modify the default Get method as follows:

[HttpGet]:

[ProducesDefaultResponseType (typeof (WeatherForecast))] public IEnumerable Get ([FromQuery] DynamicLinqDto dto) {var rng = new Random (); IQueryable query = Enumerable.Range (1,5) .Select (index = > new WeatherForecast {Date = DateTime.Now.AddDays (index), TemperatureC = rng.Next (- 20,55), Summary = Summares [rng.Next (Summaries.Length)]}) .AsQueryable (); return query.ToDynamicArray (dto);}

Because the return type of the Get method is IEnumerable, you need to use ProducesDefaultResponseTypeAttribute to specify the actual return type so that the Swagger page displays correctly:

3. Implement the extension method

The most critical code above is the ToDynamicArray method.

In fact, this is our encapsulated extension method to implement an advanced query for any IQueryable object:

Public static class DynamicLinqExtentions {public static dynamic [] ToDynamicArray (this IQueryable query, DynamicLinqDto dto) {if (! string.IsNullOrWhiteSpace (dto.Fields)) {query = query.Select ($@ "new ({dto.Fields})");} if (! string.IsNullOrWhiteSpace (dto.Filter)) {query = query.Where (dto.Filter) } if (! string.IsNullOrWhiteSpace (dto.OrderBy)) {query = query.OrderBy (dto.OrderBy);} var pageNo = dto.PageNo? 1; var pageSize = dto.PageSize? 10; query = query.Page (pageNo, pageSize); return query.ToDynamicArray ();}} 4. Effect.

Run the program, pass in the specified parameters and execute:

As you can see, the Advanced query service is now complete:

This is the end of this article on "how to create an advanced query service for ynamic LINQ". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report