Cybercop
Cybercop

Reputation: 8674

Cannot convert IQueryable<IEnumerable<string>> to return type IEnumerable<string>

In the following function I get an error when I try to return a value saying:

Cannot convert System.Linq.IQueryable<System.Collections.Generic.IEnumerable<string>> to return type System.Collections.Generic.IEnumerable<string>

public IEnumerable<string> GetModuleKindPropertyNames(long moduleKindId)
{
    var configurationId = GetModuleKindPertypeConfigurationId(moduleKindId);
    var propertyNames = _dbSis.ModuleKinds
                              .Where(t => t.PerTypeConfigurationId == configurationId)
                              .Select(x => x.PerTypeConfiguration.Properties
                              .Select(z => z.Name));

    return propertyNames; //red line below property names
}

How can I solve this issue?

Upvotes: 0

Views: 283

Answers (1)

Enrico Campidoglio
Enrico Campidoglio

Reputation: 59923

It looks like you want to select items from a collection within a collection and flatten the result into a single sequence.

Conceptually, something like:

Example class diagram

If that's the case, you're looking for the SelectMany method:

var propertyNames = _dbSis.ModuleKinds
                          .Where(t => t.PerTypeConfigurationId == configurationId)
                          .SelectMany(x => x.PerTypeConfiguration.Properties)
                          .Select(z => z.Name);

Upvotes: 1

Related Questions