Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在C/Linq中获取列名的值,然后将这些值放入您自己的类中_C#_Linq - Fatal编程技术网

C# 如何在C/Linq中获取列名的值,然后将这些值放入您自己的类中

C# 如何在C/Linq中获取列名的值,然后将这些值放入您自己的类中,c#,linq,C#,Linq,这与这个问题非常相似: 不同之处在于,我正在尝试填写GraphPoint类的列表: public class GraphPoint { public DateTime DateTimePoint { get; set; } public double ValuePoint { get; set; } public GraphPoint() { } public GraphPoint(DateTime dateTimePoint, doubl

这与这个问题非常相似:

不同之处在于,我正在尝试填写GraphPoint类的列表:

public class GraphPoint
{
    public DateTime DateTimePoint { get; set; }
    public double ValuePoint { get; set; }

    public GraphPoint()
    {

    }

    public GraphPoint(DateTime dateTimePoint, double valuePoint)
    {
        DateTimePoint = dateTimePoint;
        ValuePoint = valuePoint;
    }
}
下面是一些我正在尝试使用的代码:

List<GraphPoint> graphPoints = null;
ParameterExpression doubleExpression = Expression.Parameter(typeof(double), "p");
Expression<Func<DirD, double>> column = Expression.Lambda<Func<DirD, double>> 
    (Expression.PropertyOrField(doubleExpression, file.Title), doubleExpression);
IEnumerable<double> things = context.DirDs.Select(column);
graphPoints = context.Dirds.Select(s => new GraphPoint(s => Time, column)).ToList();
我想我已经非常接近我需要做的事情了……但只是在确切的语法方面遇到了问题。
谢谢

您可以使用动态构建lambda表达式,该表达式将获取实体上属性的值。类似的内容适用于任何实体类型上的任何属性类型

// T is the entity type, TProp is the poperty type
private static Func<T, TProp> Getter<T, TProp>(string propertyName)
{
    Type entityType = typeof(T);
    PropertyInfo propertyInfo = entityType.GetProperty(propertyName);

    var getterMethodInfo = propertyInfo.GetGetMethod();
    var entity = Expression.Parameter(entityType);
    var getterCall = Expression.Call(entity, getterMethodInfo);

    var lambda = Expression.Lambda(getterCall, entity);
    var functionThatGetsValue = (Func<T, TProp>)lambda.Compile();

    return functionThatGetsValue; 
}

你到底想在这里完成什么?只是创建一个初始化GraphPoint对象的新列表?您发布的代码中有什么不起作用?因此,您有一个可枚举的double来设置为ValuePoint。DateTimePoint的vale从何而来?它是一个单独的可枚举项吗?你能发布一些样本数据吗?我需要这些图表点的列表。我需要从数据库中的表中获取这个。我知道我将使用时间列,但是直到运行时我才知道列名是什么。所以我需要在运行时将我想要的列的字符串名称更改为实际列。context.Drad.Selects=>newgraphpoints=>Time,column.ToList;不正确。假设我的表中有数据时间为11:30、12:00、9:00的列A 1.1、2.2、3.3 ColumnB 4.2、2、1.2我直到运行时才知道是否要使用ColumnA或ColumnB中的数据。但假设在运行时我想使用ColumnB。然后我想使用{11:30,4.2},{12:00,2},{9:00,1.2}作为我的图形点列表中的值。是否只有少量的列可以选择?编译时是否知道列的可能名称?嘿,谢谢!刚刚测试过,效果很好!我想我必须在表达式树上跟上速度。
static void Main()
{
    //context.Dirds
    List<Row> Dirds = new List<Row> { 
            new Row {
            Time = DateTime.Parse("2014-01-01 11:30"),
            ColumnA = 1.1,
            ColumnB = 4.2
        },

        new Row {
            Time = DateTime.Parse("2014-01-01 12:00"),
            ColumnA = 2.2,
            ColumnB = 2.0
        },

        new Row {
            Time = DateTime.Parse("2014-01-01 09:00"),
            ColumnA = 3.3,
            ColumnB = 1.2
        }
    };

    // Build a "getter" for the column that contains the double
    var getter = Getter<Row, double>("ColumnB");

    var results = Dirds.Select(r => new GraphPoint {
        DateTimePoint = r.Time,
        ValuePoint = getter(r)
    });

    foreach(GraphPoint point in results)
        Console.WriteLine("{0} {1}", point.DateTimePoint, point.ValuePoint);

}
public class Row
{
    public DateTime Time { get; set; }
    public double ColumnA { get; set; }
    public double ColumnB { get; set; }
}

public class GraphPoint
{
    public DateTime DateTimePoint { get; set; }
    public double ValuePoint { get; set; }
}