简体   繁体   中英

Get all parents that have children using a c# lambda expression

-------PLEASE DISREGARD THIS POST----------

I write my posts in a text editor before I put them here and I pasted in the wrong block of text entirely.

I'm so tired I shouldn't be operating heavy machinery or keyboards.

I will mark the answer below as correct, because it certainly is.

------END DISCLAIMER------------

How would I write a lambda query for the following scenario?

All Barcodes have a Category, but not all Categories have a Barcode. 

Select all Categories that have a Barcode.

Looking for something along the lines of:

var categories = db.Categories.Where(...)

This question probably already has an answer, but I apparently lack the search terms to find it.

Thank you!

var categories = db.Categories.Where(category => category.Barcodes.Any())

或可能

var categories = db.Categories.Where(category => category.Barcode != null)
var catsWithBarCode = Categories.Where(c => c.Barcode != null);

From the description the relation seems to be: Category {1}---{0,1} Barcode

If the size of Categories is extremely large (it is always larger than Barcodes) the alternative will be more performant.

Barcodes.Select(c => c.Category);

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