0

我有两个表 A 和 B,它们完全独立,但都有一个time列,类型为DateTime.

time我需要编写什么简单的 LINQ 查询才能在一个集合中从两个表中获取 10 个最新(基于字段)记录?例如,此查询可能返回 7 条 A 记录和 3 条 B 记录 - 总共 10 条记录。

4

2 回答 2

2
var _result = tableA
              .Select(x => x.time)
              .Union(tableB.Select(y => y.time))
              .OrderByDescending(z => z.time)
              .Take(10);

SOURCE

于 2012-12-07T09:02:50.167 回答
0

我给你举个 X-Com 的例子。让您的表格存储数据以运送到 x-com 基地。让A表存储新兵,B表存储物品。您想显示已按日期运送到基地的新兵和物品列表:

    //soldiers
    public class RowA
    {
        public long Id {get;set;}
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public DateTime Date { get; set; }
    }
    //items
    public class RowB
    {
        public long Id {get;set;}
        public string ItemName { get; set; }
        public decimal Quantity { get; set; }
        public DateTime Date { get; set; }
    }

    List<RowA> TableA;
    List<RowB> TableB;

    public Form1()
    {
        InitializeComponent();
        PrepareTestData();

        int uniqueId = 0;
        var result = (from a in TableA
                      //Map soldiers to anonymous
                      select new 
                      {
                          UniqueId = uniqueId++,
                          InnerId = a.Id,
                          Name = a.FirstName + " " + a.LastName,
                          Date = a.Date,
                      })
                    .Union(from b in TableB
                           select new 
                           {
                               UniqueId = uniqueId++,
                               InnerId = b.Id,
                               Name = b.ItemName,
                               Date = b.Date,
                           }).OrderByDescending(x => x.Date).ToList();
        dataGridView1.AutoGenerateColumns = true;
        dataGridView1.DataSource = result;

    }

    void PrepareTestData()
    {
        TableA = new List<RowA>();
        for (int i = 0; i < 7; ++i)
            TableA.Add(new RowA
            {
                Id = i + 1,
                FirstName = "Name" + i,
                LastName = "Surname" + i,
                Date = DateTime.Now.AddDays(-i)
            });
        TableB = new List<RowB>();
        for (int j = 0; j < 4; ++j)
            TableB.Add(new RowB
            {
                Id = j + 1,
                ItemName = "Laser pistol",
                Quantity = 7,
                Date = DateTime.Now.AddDays(-j)
            });
    }

您还可以为输出记录创建类:

public Form1()
    {
        InitializeComponent();
        PrepareTestData();

        int uniqueId = 0;
        var result = (from a in TableA
                      //Map soldiers to ShipmentReportItem
                      select new ShipmentReportItem
                      {
                          UniqueId = uniqueId++,
                          InnerId = a.Id,
                          Name = a.FirstName + " " + a.LastName,
                          Date = a.Date,
                      })
                    .Union(from b in TableB
                           select new ShipmentReportItem 
                           {
                               UniqueId = uniqueId++,
                               InnerId = b.Id,
                               Name = b.ItemName,
                               Date = b.Date,
                           }).OrderByDescending(x => x.Date).ToList();
        dataGridView1.AutoGenerateColumns = true;
        dataGridView1.DataSource = result;
    }

如果您需要一些检查或修改记录,则一个结果类对 ASP.NET gridview 很有用。然后,当您定义类型后,您将可以轻松访问数据源项,否则您必须获取数据项类型,然后检查其属性等。从特定属性中获取值。

如果您的表 B 还存储人员(具有相同的列),则您不需要映射,因为行结构将是相同的。

于 2012-12-07T11:02:06.040 回答