Hiral Bavisi
Hiral Bavisi

Reputation: 91

Is Not Equal in linq in c#

I am using Linq in my project.

I want all teachers from viewTeachers where Mailcode is not equal to 20.

My code is

var startWithFullName = from v in Db.viewTeachers
                                            where v.FullName.StartsWith(prefix) && !v.MailCode.Equals(20)
                                            orderby v.FullName
                                            select new { v.ID, v.FullName };

                    foreach (var teacher in startWithFullName)
                    {
                        teacherTable.Rows.Add(teacher.FullName, teacher.ID);
                    }

I have written

!v.MailCode.Equals(20)

But not sure its correct on not.

Can any one tell me how can I do this?

Upvotes: 0

Views: 8648

Answers (3)

Krunal Solanki
Krunal Solanki

Reputation: 541

It will not compile. Equals use in joins and in where condition you should use !=. Hope this will clear your doubts.

Upvotes: -1

Jonas W
Jonas W

Reputation: 3250

The condition can be written as != 20..

Something like this :

var startWithFullName = from v in Db.viewTeachers 
          where v.FullName.StartsWith(prefix) && v.MailCode != 20
          orderby v.FullName 
          select new 
              { 
                 v.ID, 
                 v.FullName 
               };

Upvotes: 1

Habib
Habib

Reputation: 223187

You can simply write your condition as:

v.MailCode != 20

So your where clause should be:

where v.FullName.StartsWith(prefix) && v.MailCode != 20

Upvotes: 7

Related Questions