简体   繁体   English

无法从“System.Linq.Expressions.Expression”转换<System.Func<T, V?> &gt;&#39; 到 &#39;System.Linq.Expressions.Expression <System.Func<T, decimal> &gt;&#39;

[英]cannot convert from 'System.Linq.Expressions.Expression<System.Func<T, V?>>' to 'System.Linq.Expressions.Expression<System.Func<T, decimal>>'

Why doesn't the following compile?为什么以下不编译?

using System;
using System.Linq;
using System.Linq.Expressions;

public static class Extensions
{
    public static V? SumOrDefault<T, V>(this IQueryable<T> @this, Expression<Func<T, V>> selector)
        where V : struct, IComparable, IComparable<V>, IConvertible, IEquatable<V>, IFormattable
    {
        Expression<Func<T, V?>> nullableSelector = null; // omitted for brevity
        return Queryable.Sum<T>(@this, nullableSelector);
    }
}

It gives this error:它给出了这个错误:

error CS1503: Argument 2: cannot convert from 'System.Linq.Expressions.Expression<System.Func<T, V?>>' to 'System.Linq.Expressions.Expression<System.Func<T, decimal>>'

Two questions:两个问题:

  • Why is it trying & failing to call the decimal version of Sum<> ?为什么尝试调用Sum<>decimal版本却失败了?
  • Why does it fail to find the decimal?为什么找不到decimal? version of that function in System.Linq.Queryable ? System.Linq.Queryable中该函数的版本?
        public static decimal? Sum<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, decimal?>> selector);

If that worked, technically, you could call that SumOrDefault function with something that decimal cannot convert yet still respect your where ( string a custom "unsummable" class would, for example).如果这有效,从技术上讲,您可以使用小数无法转换但仍然尊重您的where东西调用SumOrDefault函数(例如,自定义“unsummable”类的 string )。

And it can't find the decimal?它找不到decimal? version of the function because it doesn't know which type to convert to safely.函数的版本,因为它不知道要安全地转换为哪种类型。

C# doesn't have a way to filter a type to just numerics, AFAIK, so you will have to overload them manually like it's 2001: C# 没有办法将类型过滤为仅数字,AFAIK,因此您必须像 2001 年一样手动重载它们:

public static decimal? SumOrDefault<T>(this IQueryable<T> @this, Expression<Func<T, decimal>> selector)
public static double? SumOrDefault<T>(this IQueryable<T> @this, Expression<Func<T, double>> selector)

//etc.

暂无
暂无

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

相关问题 无法将表达式类型'lambda expression'转换为返回类型'System.Linq.Expressions.Expression <System.Func <IProduct,string,bool >>' - Cannot convert expression type 'lambda expression' to return type 'System.Linq.Expressions.Expression<System.Func<IProduct,string,bool>>' 什么是System.Linq.Expressions.Expression中的TKey <Func<TSource,TKey> &gt;? - What is TKey in System.Linq.Expressions.Expression<Func<TSource,TKey>>? IQStreamable:无法从“字符串”转换为“ System.Linq.Expressions.Expression” - IQStreamable: cannot convert from 'string' to 'System.Linq.Expressions.Expression 无法从“ System.Linq.Expressions.LambdaExpression”转换为“ System.Linq.Expressions.Expression” - Cannot convert from 'System.Linq.Expressions.LambdaExpression' to 'System.Linq.Expressions.Expression System.Linq.Expressions.Expression for .OrderBy函数 - System.Linq.Expressions.Expression for .OrderBy function 如何评估System.Linq.Expressions.Expression - How to evaluate a System.Linq.Expressions.Expression 如何从System.Linq.Expressions.Expression中删除括号? - How to remove Parantheses from System.Linq.Expressions.Expression? 不能隐含转换类型System.Linq.Expression <System.Func<Object, bool> &gt;来布尔 - Cannot implicity convert type System.Linq.Expression<System.Func<Object, bool>> to bool 无法转换类型&#39;Expression <System.Func<TP,Action> &gt;”到“表达式 <System.Func<TP,BasePageElement> &gt;&#39; - Cannot convert type 'Expression<System.Func<TP,Action>>' to 'Expression<System.Func<TP,BasePageElement>>' 无法将lambda表达式转换为类型&#39;System.Linq.Expressions.Expression&#39;,因为它不是委托类型 - Cannot convert lambda expression to type 'System.Linq.Expressions.Expression' because it is not a delegate type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM