简体   繁体   English

帮助需要LINQ语法

[英]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. 我猜你还想根据你现有的Where()子句进行过滤。 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. 恕我直言,这是过去几年.NET Framework发展方向出现问题的一个很好的例子。 I cannot stand LINQ, and nor do I feel too warmly about extension methods, anonymous methods, lambda expressions, and so on. 我不能忍受LINQ,也不会对扩展方法,匿名方法,lambda表达式等感到太热情。

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. LINQ无法替代SQL,所以你(或者至少是项目)仍然需要掌握它。 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. 编写LINQ语句并不比编写SQL简单,但它确实添加了运行时处理来构建表达式树并将其“编译”为SQL语句。 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. 现在,如果使用LINQ比使用SQL更容易解决复杂问题,或者如果它意味着您不需要也知道SQL,并且如果您可以信任LINQ将始终产生足够好的SQL,它可能仍然一直值得使用。 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 当然,在老式的SQL中,语句就是这样

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. 这会表现得更好,但我会批准,对于很多事情,LINQ和更简单的存储过程之间的性能差异将是完全无关紧要的。 (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: (另一方面,性能问题确实存在潜入的趋势,因为我们经常使用完全不切实际的数据量以及与在实际生产系统中托管我们的软件的人几乎没有共同点的环境。)但是优点是只是不使用LINQ是巨大的:

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) 1)所需技能较少(因为你必须使用SQL)2)所有数据访问都可以以一种方式执行(因为你无论如何都需要SQL)3)一些控制如何获取数据而不仅仅是什么4)更少的可能性理所当然地被指责编写英国媒体报道软件(更有效率)

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. 关于自C#2.0以来引入的许多新语言功能,可以说类似的事情,尽管我很欣赏并使用它们中的一些。 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. 类型推理的“var”关键字非常适合初始化本地化 - 在同一行上两次获取相同的类型信息并不多见。 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! 扩展方法实际上只是简单的旧实用程序方法,我还没有听到任何关于为什么应该使用SAME语法的实例方法和在另一个类上调用的静态方法的任何好的解释! 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.) (如果你怀疑:如果你有一个扩展方法Bar()用于你的Foo类型,Foo.Bar()调用一个完全不同的实现,它可能会或可能不会像你的扩展方法Bar()做的那样做引入一个具有相同签名的实例方法。它将在运行时构建并崩溃。)

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. 但我真的认为任何一个开始使用LINQ的人都在浪费时间 - 除非它正准备进行MS认证考试,而AFAIU也有点远离现实。

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

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