开发者

How do I apply the LINQ to SQL Distinct() operator to a List<T>?

I have a serious(it's getting me crazy) problem with LINQ to SQL. I am developing an ASP.NET MVC3 application using c# and Razor in Visual Studio 2010.

I have two database tables, Product and Categories:

Product(Prod_Id[primary key], other attributes)

Categories((Dept_Id, Prod_Id) [primary keys], other attributes)

Obviously Prod_Id in Categories is a foreign key. Both classes are mapped using the Entity Framework (EF). I do not mention the context of the application for simplicity.

In Categories there are multiple rows containing the Prod_Id. I want to make a projection of all Distinct Prod_Id in Categories. I did it using plain (T)SQL in SQL Server MGMT Studio according to this (really simple) query:

SELECT DISTINCT Prod_Id
FROM Categories

and the result is correct. Now I need to make this query in my application so I used:

var query = _StoreDB.Categories.Select(m => m.Prod_Id).Distinct();

I go to check the result of my query by using:

query.Select(m => m.Prod_Id);

or

foreach(var item in query)
{
  item.Prod_Id;
  //other instructions
}

and it does not work. First of all the Intellisense when I attempt to write query.Select(m => m. or item.shows just suggestions about methods (such as Equals, etc...) and not properties. I thought that maybe there was something wrong with Intellisense (I guess most of you many times hoped that Intellisense was wrong :-D) but when I launch the application I receive an error at runtime.

Before giving your answer keep in mind that;

  1. I checked many forums, I tried the normal LINQ to SQL (without using lambdas) but it does not work. The fact that it works in (T)SQL means that there is something wrong with the LINQ to SQL instruction (other queries in my application work perfectly).

  2. For application related reasons, I used a List<T> variable instead of _StoreDB.Categories and I thought that was the problem. If you can offer me a solution without using开发者_JAVA百科 a List<T> is appreciated as well.


This line:

var query = _StoreDB.Categories.Select(m => m.Prod_Id).Distinct();

Your LINQ query most likely returns IEnumerable... of ints (judging by Select(m => m.Prod_Id)). You have list of integers, not list of entity objects. Try to print them and see what you got.


Calling _StoreDB.Categories.Select(m => m.Prod_Id) means that query will contain Prod_Id values only, not the entire entity. It would be roughly equivalent to this SQL, which selects only one column (instead of the entire row):

SELECT Prod_Id FROM Categories;

So when you iterate through query using foreach (var item in query), the type of item is probably int (or whatever your Prod_Id column is), not your entity. That's why Intellisense doesn't show the entity properties that you expect when you type "item."...

If you want all of the columns in Categories to be included in query, you don't even need to use .Select(m => m). You can just do this:

var query = _StoreDB.Categories.Distinct();

Note that if you don't explicitly pass an IEqualityComparer<T> to Distinct(), EqualityComparer<T>.Default will be used (which may or may not behave the way you want it to, depending on the type of T, whether or not it implements System.IEquatable<T>, etc.).

For more info on getting Distinct to work in situations similar to yours, take a look at this question or this question and the related discussions.


As has been explained by the other answers, the error that the OP ran into was because the result of his code was a collection of ints, not a collection of Categories.

What hasn't been answered was his question about how to use the collection of ints in a join or something in order to get at some useful data. I will attempt to do that here.

Now, I'm not really sure why the OP wanted to get a distinct list of Prod_Ids from Categories, rather than just getting the Prod_Ids from Projects. Perhaps he wanted to find out what Products are related to one or more Categories, thus any uncategorized Products would be excluded from the results. I'll assume this is the case and that the desired result is a collection of distinct Products that have associated Categories. I'll first answer the question about what to do with the Prod_Ids first, and then offer some alternatives.

We can take the collection of Prod_Ids exactly as they were created in the question as a query:

var query = _StoreDB.Categories.Select(m => m.Prod_Id).Distinct();

Then we would use join, like so:

var products = query.Join(_StoreDB.Products, id => id, p => p.Prod_Id,
               (id,p) => p);

This takes the query, joins it with the Products table, specifies the keys to use, and finally says to return the Product entity from each matching set. Because we know that the Prod_Ids in query are unique (because of Distinct()) and the Prod_Ids in Products are unique (by definition because it is the primary key), we know that the results will be unique without having to call Distinct().

Now, the above will get the desired results, but it's definitely not the cleanest or simplest way to do it. If the Category entities are defined with a relational property that returns the related record from Products (which would likely be called Product), the simplest way to do what we're trying to do would be the following:

var products = _StoreDB.Categories.Select(c => c.Product).Distinct();

This gets the Product from each Category and returns a distinct collection of them. If the Category entity doesn't have the Product relational property, then we can go back to using the Join function to get our Products.

var products = _StoreDB.Categories.Join(_StoreDB.Products, c => c.Prod_Id,
               p => p.Prod_Id, (c,p) => p).Distinct();

Finally, if we aren't just wanting a simple collection of Products, then some more though would have to go into this and perhaps the simplest thing would be to handle that when iterating through the Products. Another example would be for getting a count for the number of Categories each Product belongs to. If that's the case, I would reverse the logic and start with Products, like so:

var productsWithCount = _StoreDB.Products.Select(p => new { Product = p,
    NumberOfCategories = _StoreDB.Categories.Count(c => c.Prod_Id == p.Prod_Id)});

This would result in a collection of anonymous typed objects that reference the Product and the NumberOfCategories related to that Product. If we still needed to exclude any uncatorized Products, we could append .Where(r => r.NumberOfCategories > 0) before the semicolon. Of course, if the Product entity is defined with a relational property for the related Categories, you wouldn't need this because you could just take any Product and do the following:

int NumberOfCategories = product.Categories.Count();

Anyway, sorry for rambling on. I hope this proves helpful to anyone else that runs into a similar issue. ;)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜