none
What is the most efficient way to query "Is this item in the database? If not, enter it" with LINQ? RRS feed

  • Question

  • I have a collection of strings.  A list of names for example.
    I want to loop through them and if the name is not in the DB, add it.  If not, skip it.

    I know SQL has something that does kind of what I'm saying.  I'm hoping LINQ has this. 

    I thought about doing this:
    "Count how many times this name is in the database.  If zero, enter it, otherwise skip", but I am having trouble finding anything explaining even these simple aggregate count queries with LINQ. 

    Thanks. :(
     
    Thursday, November 5, 2009 11:20 PM

Answers

  • Here you are, TecmoTech. I used this online tool to convert it to VB.NET

     For Each name As String In nameCollection
         If Not dc.Table.Any(Function(t) t.Name = name) Then
             'record not found, so insert
             'insert logic
            
         End If
     Next

    And here are some nice samples in VB.NET for learning LINQ: LINQ To SQL Samples VB

    Hope that helps.

    Regards,
    Syed Mehroz Alam
    My Blog | My Articles
    • Marked as answer by TecmoTech Friday, November 6, 2009 8:04 PM
    Friday, November 6, 2009 6:18 PM

All replies

  • Hi TecmoTech,

    Here is an straight forward way to insert record if it does not exist, else skip:

    DbContext dc = new DbContext;
    foreach( string name in nameCollection)
    {
        if ( !dc.Table.Any( t=>t.Name == name ) ) //record not found, so insert
        {
            //insert logic
        }
    
    }


    Hope that helps.

    Regards,

    Syed Mehroz Alam
    My Blog | My Articles
    Friday, November 6, 2009 6:20 AM
  • Hi TecmoTech,

    Here is an straight forward way to insert record if it does not exist, else skip:

    DbContext dc = new
     DbContext;
    foreach ( string name in nameCollection)
    {
    if ( !dc.Table.Any( t=>t.Name == name ) ) //record not found, so insert
    {
    //insert logic
    }

    }


    Hope that helps.

    Regards,

    Syed Mehroz Alam
    My Blog | My Articles
    I am not too familiar with the syntax yet, I have created the loop, but I am coding in vb.net.  I can't convert that if statement properly.  Any vb.net guys know how to translate that line?  Thanks!
    Friday, November 6, 2009 4:26 PM
  • Here you are, TecmoTech. I used this online tool to convert it to VB.NET

     For Each name As String In nameCollection
         If Not dc.Table.Any(Function(t) t.Name = name) Then
             'record not found, so insert
             'insert logic
            
         End If
     Next

    And here are some nice samples in VB.NET for learning LINQ: LINQ To SQL Samples VB

    Hope that helps.

    Regards,
    Syed Mehroz Alam
    My Blog | My Articles
    • Marked as answer by TecmoTech Friday, November 6, 2009 8:04 PM
    Friday, November 6, 2009 6:18 PM
  • Thanks Syed, I bookmarked that site.  It will be very helpful in the future!

    Friday, November 6, 2009 8:04 PM