-4

我有不同的表和不同的数据,但它们都共享一个共同的CreatedDateTime 列。

是否可以在 LINQ 中select对所有 4 个表进行查询并对所有对象进行排序Created 并将它们作为列表返回?

4

2 回答 2

3

你可以做这样的事情

t1.Select(i => new {Value = (object)i, Created = i.Created})
  .Concat(t2.Select(i => new {Value = (object)i, Created = i.Created}))
  .Concat(t3.Select(i => new {Value = (object)i, Created = i.Created}))
  .Concat(t4.Select(i => new {Value = (object)i, Created = i.Created}))
  .OrderBy(i => i.Created)
  .Select(i => i.Value)
  .ToList();
于 2012-06-01T09:40:18.837 回答
1

你可以查询你的表

var q1 = from t1 select Created;
var q2 = from t2 select Created;
var q3 = from t3 select Created;
var q4 = from t4 select Created;

然后使用

var res = q1.Union(q2).Union(q3).Union(q4).OrderBy(p=>p).ToList();

或者

var q1 = (from t1 select Created)
         .Union(from t2 select Created)
         .Union(from t3 select Created)
         .Union(from t4 select Created)
         .OrderBy(p=>p).ToList();
于 2012-06-01T09:36:49.857 回答