简体   繁体   English

属性名称为 Lambda C# 中的表达式

[英]Property Names as Lambda Expression in C#

Updating question to be more clear on what i am trying to do here.更新问题以更清楚地了解我在这里要做什么。

What i am really trying to do here is use Z.EntityFramework.Extensions to BulkMerge some data into database.我在这里真正想做的是使用 Z.EntityFramework.Extensions 将一些数据批量合并到数据库中。 Specifically i am trying to create instructions in options.ColumnInputExpression which takes the Expression i mentioned above.具体来说,我正在尝试在 options.ColumnInputExpression 中创建指令,该指令采用我上面提到的表达式。 You can see an example here on dotnetfiddle.net/lwF8DZ您可以在 dotnetfiddle.net/lwF8DZ 上看到一个示例

I have been working on this for few days now and can't seem to make any progress.我已经为此工作了几天,似乎无法取得任何进展。 Here is what i have.这就是我所拥有的。

Lets say i have Customer class.假设我有客户 class。

public class Customer
{
    public int CustomerID { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public bool IsActive { get; set; }
}

And using something like this i can create a simple expression to target fields "Description" and "IsActive"并使用这样的东西我可以创建一个简单的表达式来定位字段“描述”和“IsActive”

Expression<Func<Customer, object>> fieldsToUse = c => new { c.Description, c.IsActive };

This expression is exactly what i want but i must not hardcore it, instead i have to use a List such as this:这个表达式正是我想要的,但我不能硬核它,而是我必须使用这样的列表:

List<string> fld = new List<string>();
fld.Add("Description");
fld.Add("IsActive");

So far i have managed to use this:到目前为止,我已经设法使用它:

public static Expression<Func<T, object>> GetExpression<T>(string propName)
{
    var arg = Expression.Parameter(typeof(T), "c");
    var property = Expression.Property(arg, propName);
    var conversion = Expression.Convert(property, typeof(object));
    var exp = Expression.Lambda<Func<T, object>>(conversion , new ParameterExpression[] { arg });
    return exp;
}

which i can call like this:我可以这样称呼:

var exp = GetExpression<Customer>("IsActive");

But this only gives me following:但这只会给我以下信息:

Expression<Func<Customer, object>> sample = c => c.IsActive;

How can i create the initial expression using what i have?如何使用我拥有的内容创建初始表达式?

Expression<Func<Customer, object>> sample = c => new { c.Description, c.IsActive };

Any help would be appreciated.任何帮助,将不胜感激。 Thanks in advance!提前致谢!

You don't actually need to do what you're doing to solve the problem you're trying to solve.你实际上不需要做你正在做的事情来解决你试图解决的问题。 The bulk insert options in Z.EntityFramework.Extensions includes a ColumnInputNames property you can use instead of providing an expression. Z.EntityFramework.Extensions中的批量插入选项包括一个ColumnInputNames属性,您可以使用它来代替提供表达式。 The expression syntax is probably just a convenience method that parses the expression you give it and converts it into a list of property names anyway.表达式语法可能只是一种方便的方法,它解析您给它的表达式并将其转换为属性名称列表。

context.BulkInsert(list, options => 
    options.ColumnInputNames = new List<string>{ "Name", "IsActive" });

Or, given your dynamically-generated fld list:或者,给定您动态生成的fld列表:

context.BulkInsert(list, options => options.ColumnInputNames = fld);

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

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