r3plica
r3plica

Reputation: 13367

Linq Get a list of items from a list of items

I have a service that returns an IQueryable<Criteria>. Criteria can have many Attributes and an Attribute can have one AttributeType.

Here are the model classes :

public class Criteria
{
    public int Id { get; set; }
    public int CategoryId { get; set; }
    [Required] [MaxLength(100)] public string Name { get; set; }
    [MaxLength(1000)] public string Description { get; set; }
    public bool Highlight { get; set; }
    public int Order { get; set; }

    public IList<Attribute> Attributes { get; set; }
}

public class Attribute
{
    public int Id { get; set; }
    public int CriteriaId { get; set; }
    [Required] [MaxLength(100)] public string Name { get; set; }
    [MaxLength(100)] public string Description { get; set; }
    public int Order { get; set; }

    public AttributeType Type { get; set; }
    public AttributeOperation Operation { get; set; }
    public IList<Formula> Formulas { get; set; }
}

public class AttributeType
{
    public int Id { get; set; }
    public int AttributeId { get; set; }
    public string Name { get; set; }

    public Attribute Attribute { get; set; }
}

My CriteriaService uses a base service to return IQueryable<Criteria>. The base service method looks like this:

    /// <summary>
    ///     Gets all the entities
    /// </summary>
    /// <param name="includes">Option includes for eager loading</param>
    /// <returns></returns>
    public IQueryable<T> List(params string[] includes)
    {
        // Create a query
        IQueryable<T> query = _dbEntitySet;

        // For each include, append to our query
        if (includes != null) 
             foreach (var include in includes) 
                 query = query.Include(include);

        // Return our query
        return query;
    }

Now I want to return all the AttributeTypes for a given category. So I have started creating a method which looks like this:

public List<AttributeType> List(int categoryId)
{
    var query = _criteriaService.Value.List(categoryId, "attributes.type").Select(m => m.Attributes);
    // TODO: How to get a list of Types from the Attribute
    return new List<AttributeType>();
}

But I am unsure how I can get all the AttributeTypes from each Attribute.

Can someone help?

Upvotes: 0

Views: 249

Answers (2)

MakePeaceGreatAgain
MakePeaceGreatAgain

Reputation: 37000

Just use SelectMany which flattens your list of lists into a single list:

var query = _criteriaService.Value.List(categoryId, "attributes.type")
    .SelectMany(m => m.Attributes)
    .Select(y => x.Type);

Upvotes: 1

David
David

Reputation: 10708

As above, SelectMany is generally what you want.

Notably you're better off using as much Linq code as possible, since the usual use of IQueryable over IEnumerable is because it converts into some other kind of code under the hood, such as SQL queries, and thus isn't being done step by step in C# memory.

Upvotes: 1

Related Questions