使用动态数据透视表中的匿名列添加列表

时间:2016-11-18 16:46:49

标签: c# linq join dynamic pivot

我有两个共享一个共同字段的列表。我想加入公共字段上的列表,但其中一个列表来自SQL动态数据透视表,因此该列表中的所有列名称(链接字段除外)都有未知的列名。我的问题是如何找到这些列名,以便创建新列表?

示例

class Student 
{
 int StudentID {get; set;}
 string FirstName {get; set;}
 string LastName {get; set;}
}

studentCollection 是学生的集合

我在这里使用class ReportRanking作为例子。它是从使用动态数据透视表的存储过程返回的dynamic类。所以我提前不知道列名。我使用TestScore-1TestScore-2等作为占位符来显示返回的内容。列名将包含学生所参加考试的名称。列中的值将是他们收到的分数。

class StudentTestScores
{
 int StudentID {get; set;}
 int TestScore-1 {get; set;}
 int TestScore-2 {get; set;}
 int TestScore-3 {get; set;}
 ...
}

testResultCollection 是StudentScores的集合。

+-----------+---------+----------+----------+---------+
| StudentId | History | Algebra | Geometry | Biology |
+-----------+---------+----------+----------+---------+
|     1     |    88   |    96    |    87    |    91   |
+-----------+---------+----------+----------+---------+
|     2     |    92   |    75    |    88    |    74   |
+-----------+---------+----------+----------+---------+

因为结果来自动态数据透视表,所以在编译时我不知道StudentTestScores中列的名称是什么。它们代表学生参加考试的名称。如何引用列名,以便将列表组合成新的复合列表?

var testResults = from student in studentCollection 
                    join testResult in testResultCollection 
                      on student.StudentId equals testResult.StudentId 
                    select new {
                      student.StudentId,
                      student.FirstName,
                      student.LastName,
                      testResult.XXXXXXX // Not sure how to reference the test scores
                      ...
                    }

这就是我最终需要的......

+-----------+-----------+----------+---------+---------+----------+---------+
| StudentId | FirstName | LastName | History | Algebra | Geometry | Biology |
+-----------+-----------+----------+---------+---------+----------+---------+
|     1     | Bob       | Smith    |    88   |    96   |    87    |    91   |
+-----------+-----------+----------+---------+---------+----------+---------+
|     2     | Sally     | Jenkins  |    92   |    75   |    88    |    74   |
+-----------+-----------+----------+---------+---------+----------+---------+

2 个答案:

答案 0 :(得分:2)

如果在运行时之前不知道类属性的命名,我会使用反射来获取值。

public class Student
  {
    public int StudentID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
  }

  public class StudentTestScores
  {
    public int StudentID { get; set; }
    public int TestScoreGen {get; set;}
  }

  class Program
  {

    static void Main(string[] args)
    {
      var studentCollection = new List<Student> { new Student { StudentID = 1, FirstName = "Brett", LastName = "X" }, new Student { StudentID = 2, FirstName = "John", LastName = "Y" } };
      var testResultCollection = new List<StudentTestScores> { new StudentTestScores { StudentID = 1, TestScoreGen = 94 }, new StudentTestScores { StudentID = 2, TestScoreGen = 86 } };
      var props = testResultCollection.First().GetType().GetProperties();

      //Check my properties
      props.ToList().ForEach(x => Console.WriteLine(x));

      var testResults = from student in studentCollection
                        join testResult in testResultCollection
                          on student.StudentID equals testResult.StudentID
                        select new
                        {
                          student.StudentID,
                          student.FirstName,
                          student.LastName,
                          resultName = testResult.GetType().GetProperty(props[1].Name),
                          resultValue = testResult.GetType().GetProperty(props[1].Name).GetValue(testResult, null)
                        };

      testResults.ToList().ForEach(x => Console.WriteLine($"{x.StudentID} {x.FirstName} {x.LastName} {x.resultName} {x.resultValue}"));

      Console.ReadLine();
    }
  }

更新11-22

如果属性不存在,您可能会遇到问题。在这种情况下,反射会爆炸,因为没有任何东西存在。这相当于SQL中的左连接。您可能会加入有时在那里的东西,有时候不会。在这种情况下,您只需要知道如何处理这样的事情。我已经更新了上面的如何合成它的例子。基本上我看到我有2个或更多属性,其中我没有。然后,如果我没有使用三元运算符,我会选择该怎么做。在我看来,三元运算符对于if,then,else的直接赋值是很好的。

public class Student
  {
    public int StudentID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
  }

  public class StudentTestScores
  {
    public int StudentID { get; set; }
    //public int TestScoreGen {get; set;}
  }

  class Program
  {

    static void Main(string[] args)
    {
      var studentCollection = new List<Student> { new Student { StudentID = 1, FirstName = "Brett", LastName = "X" }, new Student { StudentID = 2, FirstName = "John", LastName = "Y" } };
      //var testResultCollection = new List<StudentTestScores> { new StudentTestScores { StudentID = 1, TestScoreGen = 94 }, new StudentTestScores { StudentID = 2, TestScoreGen = 86 } };
      var testResultCollection = new List<StudentTestScores> { new StudentTestScores { StudentID = 1 }, new StudentTestScores { StudentID = 2 } };
      var props = testResultCollection.First().GetType().GetProperties();

      //Check my properties
      props.ToList().ForEach(x => Console.WriteLine(x));

      var testResults = from student in studentCollection
                        join testResult in testResultCollection
                          on student.StudentID equals testResult.StudentID
                        select new
                        {
                          student.StudentID,
                          student.FirstName,
                          student.LastName,
                          resultName = props.Count() > 1 ? testResult.GetType().GetProperty(props[1]?.Name)?.ToString() : "Nothing",
                          result = props.Count() > 1 ? testResult.GetType().GetProperty(props[1]?.Name).GetValue(testResult, null) : "0"
                        };

      testResults.ToList().ForEach(x => Console.WriteLine($"{x.StudentID} {x.FirstName} {x.LastName} {x.resultName} {x.result}"));

      Console.ReadLine();
    }
  }

答案 1 :(得分:2)

我会将您的对象合并为一个ExpandoObject,并将其作为动态返回。这样你可以照常访问它的属性(因为它是动态的),任何反射代码(如序列化为json \ csv)也能像往常一样探索它的属性。这是代码:

class Student
{
    public int StudentId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

class StudentTestScores {
    public int StudentId { get; set; }
    public int History { get; set; }
    public int Algebra { get; set; }
    public int Geometry { get; set; }
    public int Biology { get; set; }
}

static void Main(string[] args) {
    var studentCollection = new List<Student>(new [] {
        new Student() {StudentId = 1, FirstName = "Test", LastName = "Test"}, 
    });
    var testResultCollection = new List<StudentTestScores>(new [] {
        new StudentTestScores() {StudentId = 1, Algebra = 2, Biology = 5, Geometry = 3}, 
    });
    var testResults = from student in studentCollection
                      join testResult in testResultCollection
                        on student.StudentId equals testResult.StudentId
                      select Combine(student, testResult);
    Console.WriteLine(JsonConvert.SerializeObject(testResults));
    // outputs [{"StudentId":1,"FirstName":"Test","LastName":"Test","History":0,"Algebra":2,"Geometry":3,"Biology":5}]
    Console.ReadKey();
}



static dynamic Combine(params object[] objects) {            
    var exp = (IDictionary<string, object>) new ExpandoObject();
    foreach (var o in objects) {
        var dict = o as IDictionary<string, object>;
        if (dict != null) {
            foreach (var prop in dict) {
                if (!exp.ContainsKey(prop.Key)) {
                    exp.Add(prop.Key, prop.Value);
                } 
            }
        }
        else {
            foreach (var prop in o.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public)) {
                if (prop.CanRead && !exp.ContainsKey(prop.Name)) {
                    exp.Add(prop.Name, prop.GetValue(o));
                }
            }
        }
    }            
    return exp;
}