Not equal condition in Linq Query using C# in ASP.Net

Last Reply on Feb 04, 2018 10:06 PM By nagaraju60

Posted on Jan 24, 2018 04:23 AM

Hi OurTeam,

I have an issue while developing my project using Linq.

In BackEnd:

in my database i have one table 'Table1', in this table i have two columns

Column name 	   Datatype	
Name			nvarchar(10)
Status			char(1)

in my table i have below 

Name 		Status
Mango		   E
Orange		   X

In Front End:

I have One Textbox 'TxtEntry' and Button 'BtnSave' after Entering 'Orange' in 'TxtEntry' and click on 'BtnSave' i want to check whether the Name exist or not using below query.

        public string ExistOrNot(string Fruit,string Chkvalue)
        {
            using (DataContext dc = new DataContext())
            {
                var Existvalue = (from ExistData in dc.Table1.AsEnumerable()
                                 where ExistData.Frmt_Id == Name && ExistData.Status != 'E'
                                 select ExistData.Name.ToString()).FirstOrDefault();

                if (Existvalue != null)
                {
                    return Chkvalue = "1";
                }
            }

            return Chkvalue = "2";
        }

by using above query i am not getting value but if i am using "&& ExistData.Status == 'E'" i am getting value but it is not satisfied my requirement. in simple words , if the Name - status value is != 'E' then i want return value else not return any value. please help me

Posted on Jan 24, 2018 04:26 AM
nagaraju60 says:
where ExistData.Frmt_Id == Name && ExistData.Status != 'E'

 Change the above with the below code.

where ExistData.Frmt_Id == Name && ExistData.Status != "E"

Like below.

public string ExistOrNot(string Fruit, string Chkvalue)
{
    using (TestEntities dc = new TestEntities())
    {
        var Existvalue = (from ExistData in dc.Table1.AsEnumerable()
                            where ExistData.Name == Fruit && ExistData.Status != "E"
                            select ExistData.Name).FirstOrDefault();

        if (Existvalue != null)
        {
            return "1";
        }
        else
        {
            return "2";
        }
    }
}

 


Posted on Feb 04, 2018 10:06 PM

Dear @dharmendr,

Thanks for your reply,

i did mistake while writing query in my code. actually the above code is working for me. your code is working while the status datatype is nvarchar. thanks for our team support..

       public string ExistOrNot(string Fruit,string Chkvalue)
       {
           using (DataContext dc = new DataContext())
           {
               var Existvalue = (from ExistData in dc.Table1.AsEnumerable()
                                where ExistData.Frmt_Id == Name && ExistData.Status != 'E'
                                select ExistData.Name.ToString()).FirstOrDefault();
 
               if (Existvalue != null)
               {
                   return Chkvalue = "1";
               }
           }
 
           return Chkvalue = "2";
       }