简体   繁体   English

LINQ GroupBy子句System.NotSupportedException错误

[英]LINQ GroupBy Clause System.NotSupportedException error

I have this query and I faced this error when I want to check the date inside the Min() Method. 我有这个查询,当我想检查Min()方法中的日期时遇到了这个错误。 Can anyone explain the reason? 谁能解释原因? I just know a little about Expression<Func> and Func<> . 我只是对Expression<Func>Func<>有所了解。

var s2 = md.Tb_order
    .GroupBy(x => x.orderid)
    .Where(x => x.Min(p => p.date > 2))
    .Select(x => x);

System.NotSupportedException: 'The specified method 'Boolean Min[Tb_order,Boolean](System.Collections.Generic.IEnumerable 1[SqlServer_LinqTest.Tb_order], System.Func 2[SqlServer_LinqTest.Tb_order,System.Boolean])' on the type 'System.Linq.Enumerable' cannot be translated into a LINQ to Entities store expression because no overload matches the passed arguments.'* System.NotSupportedException:'类型为''的指定方法'布尔型Min [Tb_order,Boolean](System.Collections.Generic.IEnumerable 1[SqlServer_LinqTest.Tb_order], System.Func 2 [SqlServer_LinqTest.Tb_order,System.Boolean])'无法将System.Linq.Enumerable'转换为LINQ to Entities存储表达式,因为没有重载与传递的参数匹配。'*

if I check the date outside of the Method it works fine 如果我在方法之外检查日期,则可以正常工作

var s2 = md.Tb_order
    .GroupBy(x => x.orderid)
    .Where(x => x.Min(p => p.date) > 2)
    .Select(x => x);

EDITED 已编辑

When I see the explanation of the Where() method it seems there must be a parameter of Func<> . 当我看到Where()方法的解释时,似乎必须有一个Func<>的参数。 What is the relation between p.date and Func<> type? p.dateFunc<>类型之间是什么关系? Does p.date return something of IEnumerable ? p.date是否返回IEnumerable

According to this , Min is Min<TSource, TResult> . 根据 ,最小值是Min<TSource, TResult>

TSource The type of the elements of source. TSource源元素的类型。

TResult The type of the value returned by selector (in this case the min date). TResult选择器返回的值的类型(在这种情况下为最小日期)。

but you are passing a condition there which is not what is expecting, it is expecting a sequence of values to determine the minimum value of. 但您在此处传递的条件不是所期望的,而是期望值的序列确定其最小值。

Answering your question what is the relation between p.date and Func<> type? 回答您的问题p.date和Func <>类型之间是什么关系?

p.date is pointer to the attribute date for your enumerable x which is the value of your group dictionary, group result in dictionary which is <key,value> , where key is the order id, and value is a list of orders that have the same group key (order id), so when you are writing Min(p => p.date) you are asking linq to get the min date of this sequence (the value list). p.date是指向可枚举x的属性日期的指针,x是组字典的值,字典中组结果是<key,value> ,其中key是订单ID,value是具有以下内容的订单的列表相同的组键(订单ID),因此在编写Min(p => p.date)Min(p => p.date) linq获取此序列的最小日期(值列表)。

so when you are doing this Min(p => p.date) > 2 you are comparing the min value (result from min) to 2 which is fine, but when you are passing Min(p => p.date > 2) you are passing something LINQ can't translate as we previously mention it is expecting a sequence of values, not a condition. 因此,当您执行此Min(p => p.date) > 2Min(p => p.date) > 2最小值(从min的结果)与2进行比较,这很好,但是当您传递Min(p => p.date> 2)时您传递的是LINQ无法翻译的内容,因为我们之前提到过,它期望值的序列而不是条件。

Summary 摘要

if you are doing the right way, you are telling linq to group all orders based on order id, and then exclude the groups where group min date is not more than 2 (you exclude the whole group list). 如果您使用正确的方法,则告诉linq根据订单ID对所有订单进行分组,然后排除组最小日期不超过2的组(排除整个组列表)。 maybe this is what you want, 也许这就是你想要的,

or maybe you want this. 也许你想要这个。

var s2 = md.Tb_order
    .Where(x => x.date > 2)
    .GroupBy(x => x.orderid);

which will return all orders where date is more than 2, then group them. 它将返回日期大于2的所有订单,然后将它们分组。

This differs from the first one, since the first will ignore all orders belonging to the same id when there is one of them doesn't meet date > 2, but this one will ignore only the ones where date > 2. 这与第一个订单不同,因为当第一个订单不符合日期> 2时,第一个订单将忽略所有属于同一id的订单,但是此订单将仅忽略日期> 2的订单。

Can anyone explain the reason? 谁能解释原因?

Min() is an aggregate function . Min()是一个聚合函数 You cannot aggregate on a method - eg p.date > 2 . 您无法汇总方法-例如p.date > 2 You can, however aggregate on an expression - eg p.date . 但是,您可以聚合一个表达式-例如p.date

I assume you're trying to find the order, HAVING the minimal date which is greater than 2. Indeed, the Min() method can find the minimal date , but that's it. 我假设你试图找到秩序, 具有最小date是大于2。事实上, Min()方法可以找到最小的date ,但仅此而已。 It can't check other condition(s). 它无法检查其他条件。 To do that, you should restrict your records to include only orders with dates greater than whatever you're interested in, and then do the aggregation: 为此,您应该将记录限制为仅包含日期大于您感兴趣的日期的订单,然后进行汇总:

var s2 = md.Tb_order
    .Where(x => x.date > 2)
    .GroupBy(x => x.orderid)
    .Min(x => x.date);

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

相关问题 LINQ错误:GroupBy选择上出现System.NotSupportedException - LINQ error: System.NotSupportedException on GroupBy Selection linq“System.NotSupportedException”出错 - There is an error with linq “System.NotSupportedException” 按日期时间过滤 LINQ 查询导致 System.NotSupportedException 错误 - Filtering a LINQ query by DateTime causing System.NotSupportedException Error System.NotSupportedException使用DateTime吗? 在Linq to实体 - System.NotSupportedException using DateTime? in Linq to Entities LINQ嵌套查询问题-System.NotSupportedException - LINQ nested query issue - System.NotSupportedException 发生System.NotSupportedException - System.NotSupportedException occuring WebRequest System.NotSupportedException - WebRequest System.NotSupportedException Linq to EF系统不支持的异常System.NotSupportedException - Linq to EF system not supported exception System.NotSupportedException System.NotSupportedException:&#39;LINQ to Entities中不支持指定的类型成员&#39;StartDateTime&#39; - System.NotSupportedException: 'The specified type member 'StartDateTime' is not supported in LINQ to Entities 在LINQ to Entities中使用中间实体会导致System.NotSupportedException - Using an intermediate entity in LINQ to Entities leads to System.NotSupportedException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM