简体   繁体   English

在Linq中复制SQL数据

[英]Duplicates in Linq to SQL data

I have a table in a SQL database which has a ID column (auto incrementing) and is set to be the primary key. 我在SQL数据库中有一个表,该表具有一个ID列(自动递增),并被设置为主键。 The table consists of this ID and an account name. 该表包含此ID和帐户名。

I then have a bit of code which reads this table and populates a listview with the data. 然后,我有一些代码可以读取该表并使用数据填充一个listview。 The problem is, if I order by the account name - I get duplicates listed in the listview. 问题是,如果我按帐户名称排序-我会在列表视图中列出重复项。 If I order it by the ID, I don't see any duplicates. 如果按ID排序,则看不到任何重复项。

The original data in the SQL database contains no duplicate account names, so obviously that's what i'd like to see in the listview. SQL数据库中的原始数据不包含重复的帐户名,因此很明显,这就是我想要在列表视图中看到的内容。

This is the Linq i'm using to grab the data... 这是我用来获取数据的Linq ...

    public static IEnumerable<Client2> GetClientList()
    {
        return (IEnumerable<Client2>)from c in entity.Client2s
                                    orderby c.AccountName
                                    select c;
    }

And this is the code which is being used to create the listview... 这是用于创建列表视图的代码...

        // Clear the listview
        listViewClient.Items.Clear();

        // Get imported client list from database
        foreach (Client2 c in SQLHandler.GetClientList())
        {
            ListViewItemClient lvi = new ListViewItemClient(c.AccountName, c);
            listViewClient.Items.Add(lvi);
        }

As I say, if I change this to orderby c.ID then it returns data as expected. 如我所说,如果将其更改为orderby c.ID,则它将按预期返回数据。 I've also tried adding an index to AccountName. 我也尝试过向AccountName添加索引。 I do use a custom listview item subclass, but all that does is store a reference to the Client object. 我确实使用了自定义listview项子类,但所做的只是存储对Client对象的引用。

Any idea how I can resolve this? 知道我该如何解决吗?

Thanks, 谢谢,

Just to clarify for anyone else potentially reading this issue, it was programmer error. 只是为了澄清其他可能阅读此问题的人,这是程序员错误。 My data did indeed contain duplicates but because of the sort order, they weren't listed together and therefore I didn't see them when manually checking the data. 我的数据确实包含重复项,但是由于排序顺序,它们没有一起列出,因此在手动检查数据时我没有看到它们。 It was only when I started displaying the ID that I realised they weren't sequential. 直到我开始显示ID时,我才意识到它们不是顺序的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM