admin管理员组文章数量:1023794
Refer the following code
public class Author
{
public string Name { get; set; }
public ICollection<Book> Books { get; set;}
}
public class Book
{
public string Name { get; set; }
public int AuthorId { get; set; }
public virtual Author Author { get; set; }
}
[HttpGet]
public async Task<IActionResult> GetAuthors([FromQuery] GridifyQuery query)
{
var authors = DbContext.Authors
.AsNoTracking()
.ApplyFiltering(query);
var authorsDto = await ObjectMapper.ProjectTo<AuthorDto>(authors)
.ToListAsync();
return Ok(authorsDto);
}
Problem
I want to directly fetch list of authors from Authors
table by applying filter on property Author.Books
, which is not possible at the moment (i.e. filtering on ICollection<T>
).
I am aware of the workaround where I can fetch the list of Authors by querying the Books
table and then navigating back to Authors
. However, I would like to avoid this approach and query the Authors
table directly.
I tried making Custom filters, but I am having difficulty integrating LINQ queries into the filtering logic.
References
- EF Core include an ICollection with a filter on the collection's referenced object
Refer the following code
public class Author
{
public string Name { get; set; }
public ICollection<Book> Books { get; set;}
}
public class Book
{
public string Name { get; set; }
public int AuthorId { get; set; }
public virtual Author Author { get; set; }
}
[HttpGet]
public async Task<IActionResult> GetAuthors([FromQuery] GridifyQuery query)
{
var authors = DbContext.Authors
.AsNoTracking()
.ApplyFiltering(query);
var authorsDto = await ObjectMapper.ProjectTo<AuthorDto>(authors)
.ToListAsync();
return Ok(authorsDto);
}
Problem
I want to directly fetch list of authors from Authors
table by applying filter on property Author.Books
, which is not possible at the moment (i.e. filtering on ICollection<T>
).
I am aware of the workaround where I can fetch the list of Authors by querying the Books
table and then navigating back to Authors
. However, I would like to avoid this approach and query the Authors
table directly.
I tried making Custom filters, but I am having difficulty integrating LINQ queries into the filtering logic.
References
- https://alirezanet.github.io/Gridify/guide/filtering#custom-operators
- EF Core include an ICollection with a filter on the collection's referenced object
1 Answer
Reset to default 1A simple working demo you could follow:
[HttpGet]
public async Task<IActionResult> GetAuthors([FromQuery] GridifyQuery query)
{
//query for example: "BookName=book1"
var mapping = new GridifyMapper<Author>()
.AddMap("BookName", x => x.Books.Select(c => c.Name));
var authors = _context.Author
.AsNoTracking()
.Include(a => a.Books)
.ApplyFiltering(query, mapping).ToList();
//do your stuff...
return Ok(authors);
}
The request url should be:https://localhost:portNumber/home/getauthors?filter=BookName=book1
, book1
is the name of the Book.
Refer the following code
public class Author
{
public string Name { get; set; }
public ICollection<Book> Books { get; set;}
}
public class Book
{
public string Name { get; set; }
public int AuthorId { get; set; }
public virtual Author Author { get; set; }
}
[HttpGet]
public async Task<IActionResult> GetAuthors([FromQuery] GridifyQuery query)
{
var authors = DbContext.Authors
.AsNoTracking()
.ApplyFiltering(query);
var authorsDto = await ObjectMapper.ProjectTo<AuthorDto>(authors)
.ToListAsync();
return Ok(authorsDto);
}
Problem
I want to directly fetch list of authors from Authors
table by applying filter on property Author.Books
, which is not possible at the moment (i.e. filtering on ICollection<T>
).
I am aware of the workaround where I can fetch the list of Authors by querying the Books
table and then navigating back to Authors
. However, I would like to avoid this approach and query the Authors
table directly.
I tried making Custom filters, but I am having difficulty integrating LINQ queries into the filtering logic.
References
- EF Core include an ICollection with a filter on the collection's referenced object
Refer the following code
public class Author
{
public string Name { get; set; }
public ICollection<Book> Books { get; set;}
}
public class Book
{
public string Name { get; set; }
public int AuthorId { get; set; }
public virtual Author Author { get; set; }
}
[HttpGet]
public async Task<IActionResult> GetAuthors([FromQuery] GridifyQuery query)
{
var authors = DbContext.Authors
.AsNoTracking()
.ApplyFiltering(query);
var authorsDto = await ObjectMapper.ProjectTo<AuthorDto>(authors)
.ToListAsync();
return Ok(authorsDto);
}
Problem
I want to directly fetch list of authors from Authors
table by applying filter on property Author.Books
, which is not possible at the moment (i.e. filtering on ICollection<T>
).
I am aware of the workaround where I can fetch the list of Authors by querying the Books
table and then navigating back to Authors
. However, I would like to avoid this approach and query the Authors
table directly.
I tried making Custom filters, but I am having difficulty integrating LINQ queries into the filtering logic.
References
- https://alirezanet.github.io/Gridify/guide/filtering#custom-operators
- EF Core include an ICollection with a filter on the collection's referenced object
-
Sorry I didn't get that. Gridify enables filtering data directly by passing filters in the query-params(of the URL) itself, eliminating the need to write specific queries in your action methods. However, it does have some limitations—one of them being that it cannot filter on
ICollection<T>
. I want to create a custom filter for this scenario. – AkshatSparrow Commented Nov 20, 2024 at 6:58 - Maybe you should show how you tried to make a custom filter. – Gert Arnold Commented Nov 20, 2024 at 9:44
1 Answer
Reset to default 1A simple working demo you could follow:
[HttpGet]
public async Task<IActionResult> GetAuthors([FromQuery] GridifyQuery query)
{
//query for example: "BookName=book1"
var mapping = new GridifyMapper<Author>()
.AddMap("BookName", x => x.Books.Select(c => c.Name));
var authors = _context.Author
.AsNoTracking()
.Include(a => a.Books)
.ApplyFiltering(query, mapping).ToList();
//do your stuff...
return Ok(authors);
}
The request url should be:https://localhost:portNumber/home/getauthors?filter=BookName=book1
, book1
is the name of the Book.
本文标签: cHow to Apply Gridify Filtering on an ICollectionltTgt in EF CoreStack Overflow
版权声明:本文标题:c# - How to Apply Gridify Filtering on an ICollection<T> in EF Core? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745565902a2156446.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
ICollection<T>
. I want to create a custom filter for this scenario. – AkshatSparrow Commented Nov 20, 2024 at 6:58