artos
artos

Reputation: 1

how to get entity objects using the related objects in where clause

I have the fallowing tables: Companies ---> RelCompanyTax ---> CheckList I want to get the companies which RelCompanyTax.CheckList.Month = 5. So the question is: How can I get the needed companies from DB using the LINQ.

for example I've tried to create query like this:

var query = from comps in _context.Companies
        where comps.RelCompanyTax.CheckList.Month = 5
        select comps;
List<Company> resultCompanies = query.ToList();

but I can't because this is wrong syntax. Please help me to write with correct syntax if it is possible.

For more information I want to say I've client <--> service architecture, so the entity is in service side and the problem is not in double assign symbol which is comparison predicate, Yes you are right I've syntax error in this topic because I'd not copied the code from my source. I've wrote comps.RelCompanyTax.CheckList.Month = 5 directly to this topic. My final result is to get all companies with comps.RelCompanyTax.CheckList.Month == 5 criteria. The problem is in System.Data.Objects.DataClasses.EntityCollection. I've copied the error messages below after compiling code:

Error 4 Cannot convert lambda expression to type 'string' because it is not a delegate type CompanyService.cs 20 25 TAService

Error 5 Delegate 'System.Func' does not take 1 arguments CompanyService.cs 20 25 TAService

-----------> MOST IMPORTANT ERROR MESSAGE: ---------------->

Error 6 'System.Data.Objects.DataClasses.EntityCollection' does not contain a definition for 'CheckList' and no extension method 'CheckList' accepting a first argument of type 'System.Data.Objects.DataClasses.EntityCollection' could be found (are you missing a using directive or an assembly reference?) CompanyService.cs

After getting companies without where clause I can access to the CheckList property by the way List<RelCompanyTax> relCompanyTaxes = currentCompany.RelCompanyTaxes.ToList(); int checkListCount = relCompanyTaxes[0].CheckLists.Count;

But when I trying to use the CheckList property in where clause the 4,5 and 6 errors are occurred.

Upvotes: 0

Views: 446

Answers (2)

BrokenGlass
BrokenGlass

Reputation: 160852

You are missing an equals sign = - single = is assignment, double == is comparison (which is what you want):

var query = from comps in _context.Companies
            where comps.RelCompanyTax.CheckList.Month == 5
            select comps;

Upvotes: 2

Vlad
Vlad

Reputation: 35584

I think you just need

comps.RelCompanyTax.CheckList.Month == 5

Upvotes: 2

Related Questions