Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. [SOLVED] If i Wanted to check the values of two columns against the current object how would i do it with linq to sql ? [modified]

[SOLVED] If i Wanted to check the values of two columns against the current object how would i do it with linq to sql ? [modified]

Scheduled Pinned Locked Moved LINQ
databasecsharplinqquestionannouncement
3 Posts 2 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tonyonlinux
    wrote on last edited by
    #1

    var authors = (Author)this.bookBindingSource.Current; // get the authors object
    using (var db = new BooksDataContext())
    {
    var OriginalAuthor = db.Authors.Where(af => af.AuthorFirst == authors.AuthorFirst)
    db.Authors.Where(al => al.AuthorLast == authors.AuthorLast);

          if (originalAuthor != null)
                {
                    originalAuthor.AuthorFirst = authors.AuthorFirst;
                    originalAuthor.AuthorLast = authors.AuthorLast;
                    
                }
                else
                {
                    
                   db.Authors.InsertOnSubmit(authors);
                }
                     db.SubmitChanges();
             }
          }
    
             {
    

    ****What I'm trying to do here is get an object pasted back to OriginalAuthor that contains the row where the AuthorsFirstname Column is equal to the current entity state's AuthorFirstname and also for the AuthorsLast compare the same. Then if it exist I want to simply ignore the update, otherwise I want to add it to the database... I'm confused on the whole double where clause :confused:

    modified on Tuesday, February 2, 2010 4:23 PM

    S 1 Reply Last reply
    0
    • T tonyonlinux

      var authors = (Author)this.bookBindingSource.Current; // get the authors object
      using (var db = new BooksDataContext())
      {
      var OriginalAuthor = db.Authors.Where(af => af.AuthorFirst == authors.AuthorFirst)
      db.Authors.Where(al => al.AuthorLast == authors.AuthorLast);

            if (originalAuthor != null)
                  {
                      originalAuthor.AuthorFirst = authors.AuthorFirst;
                      originalAuthor.AuthorLast = authors.AuthorLast;
                      
                  }
                  else
                  {
                      
                     db.Authors.InsertOnSubmit(authors);
                  }
                       db.SubmitChanges();
               }
            }
      
               {
      

      ****What I'm trying to do here is get an object pasted back to OriginalAuthor that contains the row where the AuthorsFirstname Column is equal to the current entity state's AuthorFirstname and also for the AuthorsLast compare the same. Then if it exist I want to simply ignore the update, otherwise I want to add it to the database... I'm confused on the whole double where clause :confused:

      modified on Tuesday, February 2, 2010 4:23 PM

      S Offline
      S Offline
      Syed Mehroz Alam
      wrote on last edited by
      #2

      Hi, You can join multiple conditions using && or ||. Also, I would prefer the use of Any method for this scenario:

      bool exists = db.Authors.Any(af => af.AuthorFirst == authors.AuthorFirst && af.AuthorLast == authors.AuthorLast);

      if (!exists) ...

      Regards,

      Syed Mehroz Alam My Blog | My Articles
      Computers are incredibly fast, accurate, and stupid; humans are incredibly slow, inaccurate and brilliant; together they are powerful beyond imagination. - Albert Einstein

      T 1 Reply Last reply
      0
      • S Syed Mehroz Alam

        Hi, You can join multiple conditions using && or ||. Also, I would prefer the use of Any method for this scenario:

        bool exists = db.Authors.Any(af => af.AuthorFirst == authors.AuthorFirst && af.AuthorLast == authors.AuthorLast);

        if (!exists) ...

        Regards,

        Syed Mehroz Alam My Blog | My Articles
        Computers are incredibly fast, accurate, and stupid; humans are incredibly slow, inaccurate and brilliant; together they are powerful beyond imagination. - Albert Einstein

        T Offline
        T Offline
        tonyonlinux
        wrote on last edited by
        #3

        THANKS that worked great!!

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups