Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 266987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T23:29:19+00:00 2026-05-11T23:29:19+00:00

I have problems binding both a telerik RadGrid and a plain vanilla ASP.NET GridView

  • 0

I have problems binding both a telerik RadGrid and a plain vanilla ASP.NET GridView to the results of the following LINQ to entities query. In both cases, the grids contain the correct number of rows, but the data from only the first handful of rows is duplicated in all the other rows. I’m directly assigning the return value from this code to the DataSource property on the grids.

public IEnumerable<DirectoryPersonEntry> FindPersons(string searchTerm)
{
    DirectoryEntities dents = new DirectoryEntities();
    return from dp in dents.DirectoryPersonEntrySet
           where dp.LastName.StartsWith(searchTerm) || dp.Extension.StartsWith(searchTerm)
           orderby dp.LastName, dp.Extension
           select dp;
}

This is the alternate plain ADO.NET code that works:

    DataTable ret = new DataTable();
    using (SqlConnection sqn = new SqlConnection(ConfigurationManager.ConnectionStrings["WaveAdo"].ConnectionString))
    {
        SqlDataAdapter adap = new SqlDataAdapter("select * from DirectoryPersonList where LastName like '" + searchTerm + "%' order by LastName ", sqn);
        sqn.Open();
        adap.Fill(ret);
    }
    return ret;
  1. The query sent to SQL Server by LINQ works.
  2. Iterating the LINQ query results before returning them results in the same duplications.
  3. Iterating the LINQ results in the calling method, before binding, results in the same duplications.

Based on the very logical and fitting advice from Marc Gravel below, I found that the EF designer had made a bad guess at an Entity Key for my entity class: the first field in its list of fields, Department, of which there are only about seven entries shared across all other records.

This is indeed the cause of the duplication. If only I could change or remove the entity key, but I cannot.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T23:29:19+00:00Added an answer on May 11, 2026 at 11:29 pm

    It looks to me like you have a borked primary key. The “identity management” aspect of LINQ-to-SQL and EF means that it is obliged to give you back the same instance whenever it sees the same primary key value(s) for the same object type.

    For example, given the data:

    id     | name       | ...
    -------+------------+------
    1      | Fred       | ...
    2      | Barney     | ...
    1      | Wilma      | ...
    1      | Betty      | ...
    

    Then if it thinks id is a primary key when iterating over the objects from LINQ, it is forced to give you “Fred”, “Barney”, “Fred”, “Fred”. Essentially, when it sees id 1 again, it doesn’t even look at the other columns – it simply fetches the instance with id 1 from the identity cache – and gives you the same Fred instance it gave you previously. If it doesn’t think id is a primary key, it will treat each row as a separate object (and so what if it has the same value in one of the fields as another record – that isn’t exactly unusual).

    I would advise checking that any fields you have marked as a primary key (in your DBML/EDM model) really are unique per row. In the case above, the id column clearly doesn’t represent a unique identifier, so is not suitable as a primary key. Just unmark it as such in the LINQ-to-SQL / EF designer.


    update: in particular, look at the “Entity Key” property for the various properties in the designer – especially if you are querying a view. Check that “Entity Key” is only set to true for suitable columns (i.e. those that make the row unique). If it is set incorrectly, set it to false. This is also visible as the yellow key icon – this should only appear on things that genuinely are unique identifiers for a record.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.