字段名称列表作为字符串数组-LINQ表达式 [英] A List of Field Names as a String Array - LINQ Expressions

查看:103
本文介绍了字段名称列表作为字符串数组-LINQ表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好MVC和LINQ专家

Hello MVC and LINQ Experts,

我有一个看起来像这样的模型:

I have a Model that looks like this:

 public class SomeClass : IValidatableObject
 {
    public string SomeString { get; set; }
    public string SomeString2 { get; set; }
    public int SomeInteger { get; set; }

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
       //... IF there is some error...THEN
       yield return new ValidationResult("Some Error Message.", GetFieldNames(() => new []{ this.SomeString }));
    }

 }

如您所见,我正在调用GetFieldNames,它接受一个表达式,并以字符串数组的形式将表达式成员返回给您.根据我最近读过的一本书,将错误链接到字段的方法是将其作为字符串传递,如下所示:

As you can see, I am calling GetFieldNames that takes an expression, and returns to you the expression members as a string array. According to a book I read recently, the way to link an error to a field is to pass it as a string as follows:

  yield return new ValidationResult("Some Error Message.", new []{ "SomeString" }));

但是我想成为强类型的,所以这是我写的方法:

But I wanted to be Strongly Typed, so here is the method that I wrote:

  public static string[] GetFieldNames(Expression<Func<object[]>> exp)
    {
        //Build a string that will in the end look like this: field1,field2,field3
        //Then we split(',') it into an array and return that string array. 
        string fieldnames = "";

        MemberExpression body = exp.Body as MemberExpression;

        if (body == null)
        {   
            NewArrayExpression ubody = (NewArrayExpression)exp.Body;
            foreach(MemberExpression exp2 in ubody.Expressions)
            {
                fieldnames += exp2.Member.Name + ",";
            }
            fieldnames = fieldnames.TrimEnd(',');

        }
        if(fieldnames.Length > 0)
            return fieldnames.Split(',');
        else
            return new string[]{};
    }

当前用法:

GetFieldNames(() => new[] { this.SomeString , this.SomeString2 });

输出:

{ "SomeString" , "SomeString2" }

这很好.

问题是,如果我按如下方式使用它,则会给我一个错误(编译时):

The problem is that if I use it as follows, it gives me an error (compile time):

GetFieldNames(() => new[] { this.SomeString , this.SomeInteger });

错误: No best type found for implicitly-typed array

我想要的输出:

{ "SomeString" , "SomeInteger" }

因为int不是复杂类型,所以我无法传递对象数组.

I can't pass in an array of object because int is not a complex type.

如何将intstring的表达式数组传递给函数?

How can I pass the function an expression array with int and string?

推荐答案

您可以尝试传递对象数组(这是您的表达式所期望的),而不是尝试使用数组初始化器语法:

You could try passing an array of objects (which is what your expression expects) instead of trying to use an array initializer syntax:

GetFieldNames(() => new object[] { this.SomeString, this.SomeInteger });

这允许您传递任意对象类型.

This allows you to pass arbitrary object types.

这篇关于字段名称列表作为字符串数组-LINQ表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆