mike
mike

Reputation: 67

Linq: select parent based on sub collection

I have a type "Download" has a collection of "IEnumerable" and are trying to return a collection of downloads where a product in the collection matches condition. This below is my attempt thus far. I think the problem is I need to select the parent, as I receive cast errors subtypeA wont cast to parent etc.

    public static IEnumerable<Download> GetDownloadsBasedOnProductId(int prodid)
    {
        var downloads =
            (IEnumerable<Download>)
            MyDataContext.Instance.Downloads.SelectMany(
                    x => x.bmdAType).Where(
                                     a => a.Id == prodid);
        return downloads;
    }

Any ideas on how to return the correct type when querying a collection of subitems?

Upvotes: 0

Views: 580

Answers (1)

dtb
dtb

Reputation: 217233

Are you looking for something like this?

public static IEnumerable<Download> GetDownloadsBasedOnProductId(int prodid)
{
    return MyDataContext.Instance
                        .Downloads
                        .Where(download => downloads.Any(a => a.Id == prodid));
}

Upvotes: 1

Related Questions