I have a table that looks somewhat like this:
| FruitID | BasketID | FruitType |
I’m passing in the query a list of BasketIDs and I want the list of FruitIDs that are within the BasketID AND that are only of a certain FruitType (values can only 1 or 2).
This is what I have:
var TheQuery = (from a in MyDC.MyTable
where TheBasketIDs.Contains(a.BasketID) &&
a.FruitType == 1 // need help here
select a.FruitID).ToList();
I’m having some difficulty expressing the second where condition. I want the FruitIDs where all the FruitType are all 1s and none are 2s.
| FruitID | BasketID | FruitType |
| 23 | 2 | 1 |
| 23 | 5 | 1 |
| 19 | 2 | 1 |
| 19 | 5 | 2 |
For instance, Fruit 23 is ok because its FruitType is always 1 but Fruit 19 isn’t ok because it also has a FruitType of 2, even if the list of TheBasketIDs I’m passing in doesn’t contain a 5.
One way to do this would be to group by fruit id, and then examine the resultant groups with LINQ expressions:
This produces the list of
FruitIDs of your desired type.EDIT: (in response to a comment below)
Then you can simplify your query as follows: