SARAVANAN PONNUSAMY
SARAVANAN PONNUSAMY

Reputation: 67

How to solve this Error in Linq : Operator '!=' cannot be applied to operands of type 'int' and 'System.Linq.IQueryable<int>'

var ll = from a in db.EmployeeMasters
         where a.EmployeeID != (from d in db.EmployeeMasters
                                join c in db.PerformanceDetails on d.EmployeeID equals c.EmployeeID
                                join z in db.ProjectMasters on c.ProjectID equals z.ProjectID
                                into ss
                                from z in ss.DefaultIfEmpty()
                                where z.ProjectName == name || z.ProjectName == name1
                                select d.EmployeeID)
         select a.EmployeeName;

It returns an error messages like below

Operator '!=' cannot be applied to operands of type 'int' and 'System.Linq.IQueryable'

I want to add this Linq query in http post method to view output in postman Anyone Please help me to solve this

Actual question is select employees who are not part of 2 projects like (CRM, Automation)

Part of both project employees are in another project but some of the employees not in any projects

My Entity Framework Data Model is shown here:

enter image description here

name and name1 are given parameters for project names

Upvotes: 1

Views: 1725

Answers (3)

Gert Arnold
Gert Arnold

Reputation: 109099

You're making this much harder than necessary. In the first place, you should use these navigation properties EF kindly creates for you, instead of using verbose and error-prone join statements. Doing that, it's much easier to write a greatly simplified predicate:

var ll = from a in db.EmployeeMasters
         where !a.PerformanceDetails
                 .Any(pd => pd.ProjectMaster.ProjectName == name 
                         || pd.ProjectMaster.ProjectName == name1)
         select a.EmployeeName;

This is a different query --it translates into EXISTS-- but the result is the same and the query plan may even be better. Also, it's much easier to add more predicates to it later, if necessary.

Upvotes: 2

SARAVANAN PONNUSAMY
SARAVANAN PONNUSAMY

Reputation: 67

 int[] EmployeeIDs = (from em in db.EmployeeMasters
                             join pd in db.PerformanceDetails on em.EmployeeID equals pd.EmployeeID into pdRes
                             from pdResult in pdRes.DefaultIfEmpty()
                             join pm in db.ProjectMasters on pdResult.ProjectID equals pm.ProjectID into pmRes
                             from pmResult in pmRes.DefaultIfEmpty()
                             where (pmResult.ProjectName == "Automation" || pmResult.ProjectName == "CRM Customer")
                             select em.EmployeeID
                           ).Distinct().ToArray();

        var empResult = (from em in db.EmployeeMasters
                         where !EmployeeIDs.Contains(em.EmployeeID)
                         select new
                         {
                             EmployeeName = em.EmployeeName
                         }).ToList();

Upvotes: 0

Selmir
Selmir

Reputation: 1206

Your question isn't really clear, but the error is pretty clear, you cannot compare type int and System.Linq.IQueryable<int>.

I assume your want something like this :

var ll = from a in db.EmployeeMasters
         where !(from d in db.EmployeeMasters
                join c in db.PerformanceDetails on d.EmployeeID equals c.EmployeeID
                join z in db.ProjectMasters on c.ProjectID equals z.ProjectID
                into ss
                from z in ss.DefaultIfEmpty()
                where z.ProjectName == name || z.ProjectName == name1
                select d.EmployeeID).Contains(a.EmployeeID)
         select a.EmployeeName;

Here we're looking for EmployeeIDs that do not appear in your query result.

Upvotes: 1

Related Questions