开发者

Linq: select parent based on sub collection

开发者 https://www.devze.com 2023-03-23 08:17 出处:网络
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.

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&开发者_StackOverflow中文版lt;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));
}
0

精彩评论

暂无评论...
验证码 换一张
取 消