How do I request an ICollection?

I have two models where they both include an ICollection object, something like this:

public class Business : ApplicationUser
{
    ...
    public virtual ICollection<Subcategory> Subcategories { get; set; }
    ...
}

public class Request
{
    ....
    public virtual ICollection<Subcategory> Subcategories { get; set; }
    ...
}

      

I want to query for a business and get a business whose subcategories match the queries (even one match is enough). I have something like this which I wrote as a sample and of course it doesn't work (the request is of type Request):

foreach (var subcategory in request.Subcategories)
{
    var businesses = db.Users
        .OfType<Business>()
        .Where(b => b.Subcategories
            .Where(s => s.SubcategoryName == subcategory.SubcategoryName));
}

      

So let's say we have two businesses, of which the first has football and basketball as subcategories, and the second has basketball and tennis. Then let's say our user selects the subcategories Football and Tennis, so my query should return both businesses, because the former includes football and the latter includes tennis.

Thus, not every matching subcategory needs to match, even one match is enough to accept the business as a result. How can I achieve this? If you can provide the code I would be glad.

Here is my subcategory model if you need it:

public class Subcategory
{
    public int SubcategoryID { get; set; }

    public string SubcategoryName { get; set; }
}

      

+3


source to share


2 answers


Look at the Linq method signatures. Specifically Where

Enumerable.Where<TSource> Method (IEnumerable<TSource>, Func<TSource, Boolean>)

      

expects a predicate that returns a boolean. Where

returns a collection, so you cannot hook it up to a Where clause expecting a Func that returns a boolean; which won't work.



You may need the following:

var businesses = db.Users.OfType<Business>()
    .Where(b => b.Subcategories
    .Any(s => s.SubcategoryName == subcategory.SubcategoryName));

      

Pay attention to the use Any

. Any returns a value boolean

indicating whether a record was found that matched your predicates => s.SubcategoryName == subcategory.SubcategoryName

+6


source


I would do this:

var businesses = db.Users
    .OfType<Business>()
    .Where(b => b.Subcategories.Intersect(request.Subcategories)
    .Any());

      

Abort this:

b.Subcategories.Intersect(request.Subcategories);

      

Will return a collection of those categories that exist in b.Subcategories and request.Subcategories.

therefore



b.Subcategories.Intersect(request.Subcategories).Any()

      

Returns true if there are any categories that exist in both, and false if they are not.

Finally:

db.Users.OfType<Business>()
        .Where(b => b.Subcategories.Intersect(request.Subcategories).Any());

      

Will return any business for which the previous statement returns true, i.e. any business with subcategories that match the query subcategories.

+3


source







All Articles