1

Can someone help to change to following to select unique Model from Product table

 var query = from Product in ObjectContext.Products.Where(p => p.BrandId == BrandId & p.ProdDelOn == null)
             orderby Product.Model
             select Product;

3 Answers 3

5

I'm guessing you that you still want to filter based on your existing Where() clause. I think this should take care of it for you (and will include the ordering as well):

var query = ObjectContext.Products
    .Where(p => p.BrandId == BrandId && p.ProdDelOn == null)
    .Select(p => p.Model)
    .Distinct()
    .OrderBy(m => m);

But, depending on how you read the post...it also could be taken as you're trying to get a single unique Model out of the results (which is a different query):

var model = ObjectContext.Products
    .Where(p => p.BrandId == BrandId && p.ProdDelOn == null)
    .Select(p => p.Model)
    .First();
Sign up to request clarification or add additional context in comments.

6 Comments

Thx Justin. A brand could have many models (with duplications). What I want is a list of model under the selected brand. So I guess the first sample is what I need to choose. Sorry I'm not familiar with the syntax, so is the small m you shown here I need to change to 'Model'?
@Jim - You don't need to change it at all. I just changed it so you understood that the statement was no longer working with a Product and started working with a Model.
Hmm.. Sorry I don't really understand but when I paste your code I got systax error "Cannot implicitly convert type 'System.Linq.IOrderedQueryable<string>' to 'System.Linq.JQueryable<Product>
I think I got it. I changed it to public IQueryable<string> Getxxx(int BrandId) then the error is gone.
Something wrong, as soon as I changed IQueryable<Product> to IQueryable<string>, I got over 100 errors and if reinstate then the errors disappeared right away. Can you help?
|
0

Change the & to && and add the following line: query = query.Distinct();

1 Comment

Which one you refered to? My original one or the suggested one?
-1

I'm afraid I can't answer the question - but I want to comment on it nonetheless.

IMHO, this is an excellent example of what's wrong with the direction the .NET Framework has been going in the last few years. I cannot stand LINQ, and nor do I feel too warmly about extension methods, anonymous methods, lambda expressions, and so on.

Here's why: I have yet to see a situation where either of these things actually contribute anything to solving real-world programming problems. LINQ is ceratinly no replacement for SQL, so you (or at least the project) still need to master that. Writing the LINQ statements is not any simpler than writing the SQL, but it does add run-time processing to build an expression tree and "compile" it into an SQL statement. Now, if you could solve complex problems more easily with LINQ than with SQL directly, or if it meant you didn't need to also know SQL, and if you could trust LINQ would produce good-enough SQL all the time, it might still have been worth using. But NONE of these preconditions are met, so I'm left wondering what the benefit is supposed to be.

Of course, in good old-fashioned SQL the statement would be

SELECT DISTINCT [Model] 
FROM [Product]
WHERE [BrandID] = @brandID AND [ProdDelOn] IS NULL
ORDER BY [Model]

In many cases the statements can be easily generated with dev tools and encapsulated by stored procedures. This would perform better, but I'll grant that for many things the performance difference between LINQ and the more straightforward stored procs would be totally irrelevant. (On the other hand, performance problems do have a tendency to sneak in, as we devs often work with totally unrealistic amounts of data and on environments that have little in common with those hosting our software in real production systems.) But the advantages of just not using LINQ are HUGE:

1) Fewer skills required (since you must use SQL anyway) 2) All data access can be performed in one way (since you need SQL anyway) 3) Some control over HOW to get data and not just what 4) Less chance of being rightfully accused of writing bloatware (more efficient)

Similar things can be said with respect to many of the new language features introduced since C# 2.0, though I do appreciate and use some of them. The "var" keyword with type inferrence is great for initializing locals - it's not much use getting the same type information two times on the same line. But let's not pretend this somehow helps one bit if you have a problem to solve. Same for anonymous types - nested private types served the same purpose with hardly any more code, and I've found NO use for this feature since trying it out when it was new and shiny. Extention methods ARE in fact just plain old utility methods, and I have yet to hear any good explanation of why one should use the SAME syntax for instance methods and static methods invoked on another class! This actually means that adding a method to a class and getting no build warnings or errors can break an application. (In case you doubt: If you had an extension method Bar() for your Foo type, Foo.Bar() invokes a completely different implementation which may or may not do something similar to what your extension method Bar() did the day you introduce an instance method with the same signature. It'll build and crash at runtime.)

Sorry to rant like this, and maybe there is a better place to post this than in response to a question. But I really think anyone starting out with LINQ is wasting their time - unless it's in preparation for an MS certification exam, which AFAIU is also something a bit removed from reality.

6 Comments

Well, I'm a 3GL guy so my age can somehow be figured out. Do you think it's fun for me to pick up these new technologies? ...but don't you need to survive? Yes??? go by the flow, what other choices are?
@Jim My rant is directed at MS, though I'll hasten to add that I think they've also done many great things with .NET (and SQL Server ;-)). Yes, we all need to survive. But sometimes the new technologies are fun (while still causing a lot of headache!) because they empower you to do stuff you couldn't do before. That's a very different kind of pain from what you get when you struggle for hours to do stuff you'd effortlessly do in minutes if only you didn't need to be on the latest trendy bandwagon. :)
@The Dag: You need to understand that Linq provides an abstraction around all different kinds of querying and much more. If you really want to know why, spend some time reading things like: linqpad.net/WhyLINQBeatsSQL.aspx, research.microsoft.com/en-us/um/people/emeijer/Papers/…, blogs.msdn.com/b/wesdyer/archive/2008/01/11/…, and also add channel9.msdn.com/Shows/Going+Deep/…, channel9.msdn.com/Shows/Going+Deep/…, and bit.ly/bfzKCk
Your mistake really lies in thinking that LINQ-to-SQL (which is just one provider, and not LINQ itself), is supposed to be a complete drop in replacement for SQL, which is not the case.
@Richard Did you read what I wrote? Nowhere do I even hint that it's SUPPOSED to be one thing or another. I'm saying as long as it isn't, it's useless. I suspect it was supposed to answer the old problem "we need something new". If you could use LINQ against many sources and no longer needed to learn SQL and XPath Query and so on, there would indeed be a point. As it is, I think the disadvantages far outweigh the benefits. I may be wrong, but please address my point rather than inventing claims on my behalf.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.