Check if record already exist using LINQ to sql

I'm using LINQ to sql with c#
I have a text box on a web page , after entering a value to that box
I need to validate whither if this value exist in myTable or not
Var xmyTable = (from ymyTable in objDataContext.myTables
                              Select new 

Open in new window

please help
Mohammad Alsolaimanapplication programmerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:

If you wish to just test if the value exist in the database then you can use a query like the following. Please replace the ColumnName place holder with the actual column name the data is in. The variable xexist will contain the boolean true if it does and false if it does NOT.

var xexist = (from ymyTable in objDataContext.myTables
              where ymyTable.ColumnName == TextBox.Text
              select true)FirstOrDefault();

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Chris WatsonSoftware DeveloperCommented:
Here's a variation of Fernando's solution.

var exists = objDataContext.myTables.Any(myTable => myTable.ColumnName == TextBox.Text);

Open in new window

Mohammad Alsolaimanapplication programmerAuthor Commented:
i couldn't understand this line of code!
select true)FirstOrDefault();

Open in new window

would you please explane
Fernando SotoRetiredCommented:
The question states that you want to verify whither the value exist in the table. That is a true/false type query. A query returns a collection of object to the caller and to turn that into a true or false predicate. To do that, turn a collection into a true or false value we say that if the record we are testing makes it to the select statement just return a true value for that record. That would return a collection of boolean type true. To turn the collection of true values into a single value of true we take the first element of the collection and return it. Now if the collection that is returned is empty then we need to return false. That is what the FirstOrDefault method in the query does. It will take the first element and return it, if the collection is empty it returns the default value for a boolean which is false.
Mohammad Alsolaimanapplication programmerAuthor Commented:
thanks a lot, and very sorry for late
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.