简体   繁体   中英

LINQ Query to Filter Items By Criteria From Multiple Lists

I'm having trouble conceptualizing something that should be fairly simple using LINQ. I have a collection that I want to narrow down, or filter, based on the id values of child objects.

My primary collection consists of a List of Spots. This is what a spot looks like:

public class Spot
{
    public virtual int? ID { get; set; }
    public virtual string Name { get; set; }
    public virtual string Description { get; set; }
    public virtual string TheGood { get; set; }
    public virtual string TheBad { get; set; }
    public virtual IEnumerable<Season> Seasons { get; set; }
    public virtual IEnumerable<PhotographyType> PhotographyTypes { get; set; }
}

I'm trying to filter the list of Spots by PhotographyType and Season. I have a list of ids for PhotographyTypes and Seasons, each in an int[] array. Those lists look like this:

criteria.PhotographyTypeIds //an int[]
criteria.SeasonIds //an int[]

I want to build a collection that only contains Spots with child objects (ids) matching those in the above lists. The goal of this functionality is filtering a set of photography spots by type and season and only displaying those that match. Any suggestions would be greatly appreciated.

Thanks everyone for the suggestions. I ended up solving the problem. It's not the best way I'm sure but it's working now. Because this is a search filter, there are a lot of conditions.

private List<Spot> FilterSpots(List<Spot> spots, SearchCriteriaModel criteria)
    {
        if (criteria.PhotographyTypeIds != null || criteria.SeasonIds != null)
        {
            List<Spot> filteredSpots = new List<Spot>();

            if (criteria.PhotographyTypeIds != null)
            {
                foreach (int id in criteria.PhotographyTypeIds)
                {
                    var matchingSpots = spots.Where(x => x.PhotographyTypes.Any(p => p.ID == id));
                    filteredSpots.AddRange(matchingSpots.ToList());
                }
            }

            if (criteria.SeasonIds != null)
            {
                foreach (int id in criteria.SeasonIds)
                {
                    if (filteredSpots.Count() > 0)
                    {
                        filteredSpots = filteredSpots.Where(x => x.Seasons.Any(p => p.ID == id)).ToList();
                    }
                    else
                    {
                        var matchingSpots = spots.Where(x => x.Seasons.Any(p => p.ID == id));
                        filteredSpots.AddRange(matchingSpots.ToList());
                    }
                }
            }
            return filteredSpots;
        }
        else
        {
            return spots;
        }
    }

This may helps you:

List<Spot> spots = new List<Spot>();
Spot s1 = new Spot();
s1.Seasons = new List<Season>() 
             { new Season() { ID = 1 }, 
               new Season() { ID = 2 }, 
               new Season() { ID = 3 } 
             };
s1.PhotographyTypes = new List<PhotographyType>() 
             { new PhotographyType() { ID = 1 }, 
               new PhotographyType() { ID = 2 } 
             };

Spot s2 = new Spot();
s2.Seasons = new List<Season>() 
             { new Season() { ID = 3 }, 
               new Season() { ID = 4 }, 
               new Season() { ID = 5 } 
             };
s2.PhotographyTypes = new List<PhotographyType>() 
             { new PhotographyType() { ID = 2 }, 
               new PhotographyType() { ID = 3 } 
             };

List<int> PhotographyTypeIds = new List<int>() { 1, 2};
List<int> SeasonIds = new List<int>() { 1, 2, 3, 4 };

spots.Add(s1);
spots.Add(s2);

Then:

var result = spots
             .Where(input => input.Seasons.All
                           (i => SeasonIds.Contains(i.ID)) 
                    && input.PhotographyTypes.All
                           (j =>  PhotographyTypeIds.Contains(j.ID))
                    ).ToList();
// it will return 1 value 

Assuming:

public class Season
{
    public int ID { get; set; }
    //some codes
}
public class PhotographyType
{
    public int ID { get; set; }
    //some codes
}

You have an array of IDs that has a Contains extension method that will return true when the ID is in the list. Combined with LINQ Where you'll get:

List<Spot> spots;   // List of spots
int[] seasonIDs;    // List of season IDs

var seasonSpots = from s in spots
                  where s.ID != null
                  where seasonIDs.Contains((int)s.ID)
                  select s;

You can then convert the returned IEnumerable<Spot> into a list if you want:

var seasonSpotsList = seasonSpots.ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM