Check if record already exist using LINQ to sql

Posted on 2014-07-17
Last Modified: 2016-02-15
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
Question by:M_SOLAIMAN
    LVL 62

    Accepted Solution


    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

    LVL 4

    Assisted Solution

    by:Chris Watson
    Here's a variation of Fernando's solution.

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

    Open in new window


    Author Comment

    i couldn't understand this line of code!
    select true)FirstOrDefault();

    Open in new window

    would you please explane
    LVL 62

    Assisted Solution

    by:Fernando Soto
    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.

    Author Closing Comment

    thanks a lot, and very sorry for late

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
    Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
    Via a live example, show how to setup several different housekeeping processes for a SQL Server.
    Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now