Home > front end >  How to query an array of integers ​in an integer collection in ASP.NET Core 3.1?
How to query an array of integers ​in an integer collection in ASP.NET Core 3.1?

Time:02-24

I would like to query an integer collection. For now, I can only filter by the first value in the array of integers. How can I query that includes all values ​​in the integer array for a blog tag id collection?

 //list --> my blog posts variable

 ....
 if (f.blogTagIds != null)
 {
     list = list.Where(p => p.BlogTagRelation.Select(p => p.BlogTagId).Contains(f.blogTagIds[0]));
 }
 ....

SearchFilterClass

public class SearchFilterType
{
    ...  
    public int[] blogTagIds { get; set; }
    ...
}

ViewBag BlogTags

var BlogTags = new List<SelectListItem>();

foreach (var item in _uow.BlogTag.GetAllByEnabledDate(null, _uow.Cookie.GetAdminLangId, _uow.Cookie.GetAdminWebSiteId))
{
    BlogTags.Add(new SelectListItem
                     {
                         Text = item.Title,
                         Value = item.Id.ToString()
                     });
}

ViewBag.BlogTags = BlogTags;

Filter Form BlogTagIds Select Control

<select asp-for="f.blogTagIds"  multiple="multiple" asp-items="ViewBag.BlogTags"></select>

BlogTagRelation.cs

    public partial class BlogTagRelation
{
    public int Id { get; set; }
    public int BlogId { get; set; }
    public int BlogTagId { get; set; }

    public virtual Blog Blog { get; set; }
    public virtual BlogTag BlogTag { get; set; }
}

Example route

/Blog/list?f.blogTagIds=8&f.blogTagIds=6

CodePudding user response:

Try:

var list =list.Where(x => blogTagIds.All(r => x.BlogTagRelation.Any(y => y.BlogTag.BlogTagId== r)));

make sure that All of the blogTagIds are contained in BlogTagRelation

result:

[I select user with special roleId]

enter image description here

  • Related