Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.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# 如何使用Linq2Sql将多个数据库列存储到一个数组中_C#_Sql Server_Linq To Sql_C# 3.0 - Fatal编程技术网

C# 如何使用Linq2Sql将多个数据库列存储到一个数组中

C# 如何使用Linq2Sql将多个数据库列存储到一个数组中,c#,sql-server,linq-to-sql,c#-3.0,C#,Sql Server,Linq To Sql,C# 3.0,我必须处理多个SQL Server表,这些表通常如下所示: int id_this, int id_that, ..., double Value1, double Value2, ..., double Value96 我知道这很糟糕,但我无法改变。我现在要做的是定义一些类,比如 public class Foo { public int Id_This { get; set; } public int Id_That { get; set; } ... pu

我必须处理多个SQL Server表,这些表通常如下所示:

int id_this, int id_that, ..., double Value1, double Value2, ..., double Value96
我知道这很糟糕,但我无法改变。我现在要做的是定义一些类,比如

public class Foo
{
    public int Id_This { get; set; }
    public int Id_That { get; set; }
    ...
    public double Value[];
}
值数组当然是一个属性,但我想你明白了

问题是,如何尽可能轻松地将96列放入数组中

我可以用一个普通的SqlDataReader实现这一点,因为DataRow允许索引访问,但我想知道我是否可以声明一些属性或编写一些最低数量的代码来直接将该类与LINQ2SQL一起使用

至少,我想这样做

dataContext.ExecuteQuery<Foo>("SELECT * FROM Foo");
哦,那是。。。美好的DataContext方法总是期望实体类型;没有ExecuteReader,这是一种痛苦,但可以理解,因为它希望表现为ORM。老实说,我很想使用ADO.NET来处理这个表,但是如果您已经将宽表映射到DataContext,那么您应该能够使用常规C或反射

由于数字不变,除非有多个表,否则我会咬紧牙关编写一些难看的代码:

Foo foo = new Foo { Id_This = obj.Id_This, Id_That = obj.Id_That,
    Values = new double[] {obj.Value1, obj.Value2, ... } };
如果您有多个表。。。反射,可能通过表达式优化:


以防你对96感到疑惑。每天每一刻钟有一个值:-嗨,马克,谢谢你的及时回答。我特别喜欢可重用表达式的想法。
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
class FooUgly
{
    public int IdThis { get; set; }
    public int IdThat { get; set; }
    public double Value1 { get; set; }
    public double Value2 { get; set; }
    public double Value3 { get; set; }
}
class Foo
{
    public int IdThis { get; set; }
    public int IdThat { get; set; }
    public double[] Values { get; set; }
    public Foo() { }
    internal Foo(FooUgly ugly)
    {
        IdThis = ugly.IdThis;
        IdThat = ugly.IdThat;
        Values = extractor(ugly);
    }
    // re-use this!!!
    static readonly Func<FooUgly, double[]> extractor =
        ValueExtractor<FooUgly, double>.Create("Value", 1, 3);
}
static class Program
{
    static void Main()
    {
        FooUgly ugly = new FooUgly { IdThis = 1, IdThat = 2, Value1 = 3, Value2 = 4, Value3 = 5 };
        Foo foo = new Foo(ugly);
    }
}
static class ValueExtractor<TFrom,TValue>
{
    public static Func<TFrom, TValue[]> Create(string memberPrefix, int start, int end)
    {
        if(end < start) throw new ArgumentOutOfRangeException();
        ParameterExpression param = Expression.Parameter(typeof(TFrom), "source");
        List<Expression> vals = new List<Expression>();
        for(int i = start ; i <= end ; i++) {
            vals.Add(Expression.PropertyOrField(param, memberPrefix + i));
        }
        Expression arr = Expression.NewArrayInit(typeof(TValue), vals);
        return Expression.Lambda<Func<TFrom, TValue[]>>(arr, param).Compile();
    }
}