Leron
Leron

Reputation: 9846

LINQ to Entity join query

I have the following setup:

Table ShoeAreas that has columns ShoeId and MaterialId. Table Shoes that has columns ID and Status.

I have a method that takes one argument - materialId and the goal is to determine if there is a record in ShoeAreas with a MaterialId equal to the one passed like an argument. And if such a record (or records most probably) exist if they are relateed to shoe from Shoes withStatus` = Production.

I tried this :

 return shoeService.All().
                Join(shoeAreaService.All(),
                s => s.ID,
                sa => sa.ShoeId,
                (s, sa) => (sa.MaterialId == matId)).
                Any(s => (s.Status == (byte)EntityStatusProd.Production)));

But I get error on the Any.. line saying } expected and also this is my second Linq to Entity query that I write so I have doubts if it's syntax problem or the query is wrong itself.

Upvotes: 0

Views: 5553

Answers (3)

cuongle
cuongle

Reputation: 75296

 return shoeService.All().Any(s => shoeAreaService.All()
                                .Any(sa => sa.MaterialId == matId 
                                        && s.Id == sa.ShoeId)
                          && s.Status == (byte)EntityStatusProd.Production);

Upvotes: 1

Elior
Elior

Reputation: 3266

you can try this: (linq )

from shoe in Shoes 
join shoeArea in ShoesArea on shoe.ID equals shoeArea.ShoeID
where shoeArea.MeterialID == matID && shoe.Status == (byte)EntityStatusProd.Production
select new {shoe.ID,shoe.Status};

Upvotes: 1

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236188

You are returning IEnumerable<bool> from Join method (values of condition sa.MaterialId == matId). Create anonymous type which will hold both joined entities instead:

 return shoeService.All()
           .Join(shoeAreaService.All(),
                 s => s.ID,
                 sa => sa.ShoeId,
                 (s, sa) => new { s, sa }) // here
           .Any(x => (x.sa.MaterialId == matId) && 
                     (x.s.Status == (byte)EntityStatusProd.Production)));

Upvotes: 1

Related Questions