LINQ, Verbose WHERE Clause

This is a discussion on LINQ, Verbose WHERE Clause within the C# Programming forums, part of the General Programming Boards category; Using LINQ, I updated the field Age of a Person object. When I looked at the generated SQL (from SQL ...

  1. #1
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696

    LINQ, Verbose WHERE Clause

    Using LINQ, I updated the field Age of a Person object. When I looked at the generated SQL (from SQL Server Profiler), I saw the following.

    Code:
    exec sp_executesql N'UPDATE [dbo].[Persons]
    SET [Age] = @p4
    WHERE ([PersonId] = @p0) AND ([FirstName] = @p1) AND ([LastName] = @p2) AND ([Age] = @p3)',N'@p0 int,@p1 nvarchar(4),@p2 nvarchar(5),@p3 int,@p4 int',@p0=1,@p1=N'John',@p2=N'Smith',@p3=21,@p4=22
    I was wondering why the WHERE clause has to include fields PersonId, FirstName, LastName, and Age when it could have just used the field PersonId. Is there a way to force LINQ to use primary key only?

    I created database below.

    Code:
    CREATE TABLE dbo.Persons
    (
    	PersonId INT NOT NULL IDENTITY PRIMARY KEY
    	, FirstName NVARCHAR(100) NOT NULL
    	, LastName NVARCHAR(100) NULL
    	, Age INT NOT NULL
    )
    
    
    INSERT INTO dbo.Persons
    SELECT
    	FirstName = 'John'
    	, LastName = 'Smith'
    	, Age = 21
    Then, I created the .dbml by drag-and-dropping the database to my project.

    And here's is my program.

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace LINQ
    {
        class Program
        {
            static void Main(string[] args)
            {
                Entities.DataContext context = new Entities.DataContext();
    
                int personId = 1;
                Entities.Person person = context.Persons.SingleOrDefault(p => p.PersonId == personId);
                person.Age = 22;
                context.SubmitChanges();
            }
        }
    }
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  2. #2
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    perhaps this link will help.
    Here to Deceive, Inveigle, Obfuscate Since 1945

  3. #3
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    I'm sorry. Did you forget to post the link because I don't see one?
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  4. #4
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Quote Originally Posted by alphaoide View Post
    I'm sorry. Did you forget to post the link because I don't see one?
    DOI!

    LINQ Updates using all columns in the WHERE clause instead of Primary Key

    sorry.
    Here to Deceive, Inveigle, Obfuscate Since 1945

  5. #5
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Thanks, the link really helped.
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. LINQ: How to Read XML PARAMETER Attributes
    By SeekAndFind in forum C# Programming
    Replies: 2
    Last Post: 05-06-2010, 11:18 AM
  2. problem with cin.fail() in if/else clause
    By clearcom in forum C++ Programming
    Replies: 1
    Last Post: 04-17-2010, 11:11 PM
  3. SQL like outer join using LINQ
    By indigo0086 in forum C# Programming
    Replies: 2
    Last Post: 08-12-2008, 11:27 AM
  4. if clause not working as I expect.
    By luise.valencia in forum C Programming
    Replies: 4
    Last Post: 04-16-2005, 09:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21