简体   繁体   中英

Linq query with where clause on related data

I have a data context using two SQL tables ( A and B) that are queried using the following code:

                    var query = from b in _ctx.A
                                .Where(b => b.Flag == true).Include(c => c.B)
                                select b;

The Flag column resides in the table A. I want to add an additional where clause to the query that checks for a column named Qty in table B for values > 10.

I tried the following code but got an error:

                    var query = from b in _ctx.A
                                .Where(b => b.Flag == true)
                                .Include(c => c.B.Where(i => i.Qty >= 10))
                                select b;

Error: The Include path expression must refer to a navigation property defined on the type. Use dotted paths for reference navigation properties and the Select operator for collection navigation properties. Parameter name: path

I found this reference: The Include path expression must refer to a navigation property defined on the type.in eager loading

but I don't understand how to use the select for my query.

The include Path is just there to load a property, if it is not loaded for any other reason.

The navigation collection cannot be filtered, since it's establishing the relation. It has always the meaning of "all associated entities somewhere else".

But we can reverse the query, assuming classB has a Property A of classA.

             var query = _ctx.B
                            .Where(b => b.Qty >=10 && b.A.Flag);
                            .Include(b => b.A)

The Where clause in this case will not load the property B, this is just evaluated on the server. The include here is really necessary to fill the property A.

Now you get a list of all B's with associated A. If you want your collection of A's, you can group By A

             var query = _ctx.B
                            .Where(b => b.Qty >=10 && b.A.Flag)
                            .GroupBy(x => x.A);

This will result in a collection of A, with all associated B's that have Qty>10.

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