开发者

Filter parent/child table (one to many association) in linq query based on entities in child table?

开发者 https://www.devze.com 2023-01-06 03:32 出处:网络
I have a table (Projects) which is linked to projectVersions on projectID projectVersions contains several columns on which I\'d like to filter a returned project (and associated projectVersions) lis

I have a table (Projects) which is linked to projectVersions on projectID

projectVersions contains several columns on which I'd like to filter a returned project (and associated projectVersions) list. For example there is a "capacity" column and a "country" column. I am doing a filtered list of projects on one page and I'd like to include all projects where any one of the associated projectVersions has a capacity of 750ml and a country of "France" for example.

It may be that a particular parameter is not set and so I pass a zero to indicate not to filter on that.

I guess this needs some kind of subquery as when I try and do something like this:

  thisList = (From p In dc.tblProjects _  
             Where ((Brand = 0) Or (p.Brand = Brand)) _  
             And ((brandVariant = 0) Or (p.brandVariant = brandVariant)) _  
            开发者_如何学运维 And ((sizeCapacity = 0) Or (p.tblProjectVersions.sizeCapacity.xxx = sizeCapacity)) _  
                                 Order By p.dateCreated Ascending _  
                                 Select p).ToList  

it doesn't work as the "xxx" bit, being one-to-many, expects me to specify an item to get at the entities within and yet I want to query where ANY of the associated versions match one of the criteria.


If I understand correctly, the issue is in

 Or (p.tblProjectVersions.sizeCapacity.xxx = sizeCapacity)

Since "tblProjectVersions" is a collection, and you want to find where "any" of the associated versions match the criteria, do:

 Or (p.tblProjectVersions.Any(Function(t) t.sizeCapacity.xxx = sizeCapacity))

This will check to see if there is any element inside of the table with the appropriate "sizeCapacity".

0

精彩评论

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