简体   繁体   中英

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?

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));
}

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