|
| using linq to populate ddl query issue relating to duplicate records |
 |
Thu, 3 Apr 2008 12:05:02 +0000 |
Hi
I would like to retreive a category list from my product list but there are many
products with the same category name and i get multiple entries of the same
category in my ddl. How do i change my query to avoid duplicates ?
Thanks in advance
Dim prod = From u In db4.products _
Where u.category = ddlproduct.SelectedItem.Text _
Select u.prodid, u.description
|
| Post Reply
|
| Re: using linq to populate ddl query issue relating to duplicate records |
 |
Thu, 3 Apr 2008 12:17:04 +0000 |
I don't use LINQ much in VB.Net, but you just need to call Distinct something
like this:
Dim prod = (From u In db4.products _
Where u.category = ddlproduct.SelectedItem.Text _
Select u.prodid, u.description).Distinct()
|
| Post Reply
|
| Re: using linq to populate ddl query issue relating to duplicate records |
 |
Thu, 3 Apr 2008 12:29:52 +0000 |
thanks so much. ;)
|
| Post Reply
|
| Re: using linq to populate ddl query issue relating to duplicate records |
 |
Thu, 3 Apr 2008 15:46:57 +0000 |
Using "distinct" without knowing WHY you have duplicate records coming
back from your query is a BAD idea.
You may not be solving the problem, you may just be hiding it for later, when it
will be an even bigger pain to fix.
Adding a distinct without knowing why is the logical equivalent of this:
try
{
doSomething();
}
catch
{
}
Yep, the above error does not go away, but it is still happening, and that could
be very bad.
So, WHY are you getting duplicate records? Are you querying from a table or a
view? If a view, the view will need to be fixed if it is not supposed to return
duplicate records.
If a table, exactly what is being duplicated? prodid? description? If so,
shouldn't you have primary or unique key constraints using those columns in the
database?
|
| Post Reply
|
| Re: using linq to populate ddl query issue relating to duplicate records |
 |
Thu, 3 Apr 2008 21:47:47 +0000 |
shaunie2.0:
Hi David, thanks for the response. I do know why i was getting duplicate
records back, i just needed to filter the duplicated out. Since the categories
column in the products table may have many products from the same category i
just needed to retreive all the different categories from that table.
I guess there isn't a "categories" table that you could query for that
purpose.
|
| Post Reply
|
|
|
|
|
|
|
|
|
|