简体   繁体   中英

Help Need with LINQ Syntax

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;

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();

&更改为&&并添加以下行: query = query.Distinct();

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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