English 中文(简体)
我能否用不可损耗的参数作一个表述?
原标题:Can I force an expression to be used with a non-nullable parameter?

I m not sure how to explain this, so I m gonna show you some code first and explain what it does.

Extension Filter: receives a parameter expression and a special type of filter I ve built for my application (IFilter).

public static IQueryable<TSource> Filter<TSource, TProperty>(this IQueryable<TSource> source, Expression<Func<TSource, TProperty>> expression, IFilter f)
{
    if (!f.IsEmpty())
    {
        string propertyName = ExpressionHelper.GetExpressionText(expression);
        source = source.Where(f.GetWhereLambda<TSource>(propertyName));
    }

    return source;
}

目前,有可能这样做(简化!)

var Foo = db.Foos
.Select(e => new
{
    ID = e.ID, // Primary key.
    Bar = e.BarID // Some nullable FK.
})
.Filter(e => e.Bar, this.someSpecialFilter);

因此,那里存在问题。 BarID 可能是无效的,因此,欧洲自由贸易联盟正在使用<代码>int?制作一个PPOCO级,这对我来说是罚款的,但对于我的言论处理却不然。 这个问题首先源于:。 • 如何从类型变量中产生一种可变的加固;T>?

问题描述:

The exception The binary operator Equal is not defined for the types System.Nullable`1[System.Int32] and System.Int32 . is throwed from this code:

return Expression.Equal(leftExpr, rightExpr);

离开 Expr is a System.Nullable1[System.]. Int32 (作为<代码>int?)和右翼为System。 Int32 (the Value, as Int32').

问题:

我能做些什么来避免? from int?? 我指的是,能否避免获得一种无效的类型,或者我是否在守则中对此加以核对?

感谢很多人,

Ricardo

最佳回答

我发现的目前解决办法是使用导航财产,在无法修复的情况下只是放弃一个例外。

    if ((expression.ReturnType.IsGenericType) && (expression.ReturnType.GetGenericTypeDefinition() == typeof(Nullable<>)))
    {
        throw new Exception("The property expression cannot be Nullable.");
    }

而这种使用也是这样:

var Foo = db.Foos
.Select(e => new
{
    ID = e.ID, // Primary key.
    Bar = e.Bar.ID // Use the PK from Bar s navigation property.
})
.Filter(e => e.Bar, this.someSpecialFilter);
问题回答

希望:

我是否可以避免? ?

之后,请解释:

在一些情况下,原科索沃解放党是不可否认的。

因此,我的理解是,你可以避免无效的类型。

this may be a conception issue, or i miss something there





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签