check if record exist

Discussion in 'ASP .Net' started by Seema, Jun 8, 2009.

  1. Seema

    Seema Guest

    What is the best way to check for a record exists in a database using linq to
    sql?
    I want to check if a userid (int) and typeid (int) exist. - The following
    code always returns zero even if the record exists - what is the best
    approach?

    int result = 0
    result = (from t in context.customers
    where ((t.userid == intUserID) && (t.typeid ==
    intTypeID))
    select t).Count();
     
    Seema, Jun 8, 2009
    #1
    1. Advertisements

  2. On Jun 8, 6:33 am, Seema <> wrote:
    > What is the best way to check for a record exists in a database using linq to
    > sql?
    > I want to check if a userid (int) and typeid (int)  exist. - The following
    > code always returns zero even if the record exists - what is the best
    > approach?
    >
    > int result = 0
    > result = (from t in context.customers
    >                               where ((t.userid == intUserID) && (t.typeid ==
    >  intTypeID))
    >                               select t).Count();


    var v = from t in context.customers where t.userid == intUserID &&
    t.typeid == intTypeID select t;
    bool exists = v.Count() == 0 ? false : true;
     
    Alexey Smirnov, Jun 8, 2009
    #2
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SnVzdGlu?=

    Retrieving Record Key while creating the record.

    =?Utf-8?B?SnVzdGlu?=, Oct 4, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    832
    =?Utf-8?B?SnVzdGlu?=
    Oct 5, 2004
  2. Y.S.
    Replies:
    3
    Views:
    1,307
    strajan
    Sep 17, 2003
  3. LT
    Replies:
    7
    Views:
    2,350
    Phlip
    Jul 25, 2004
  4. André
    Replies:
    0
    Views:
    3,926
    André
    Jun 25, 2006
  5. MRW
    Replies:
    0
    Views:
    509
  6. timcadieux
    Replies:
    0
    Views:
    396
    timcadieux
    Jun 26, 2008
  7. basi
    Replies:
    2
    Views:
    189
  8. Markus Mohr
    Replies:
    7
    Views:
    518
    Thomas 'PointedEars' Lahn
    Nov 28, 2003
Loading...