LINQ - 如何从两个表中获取n条记录?

时间:2012-12-07 08:58:16

标签: c# .net database linq

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

在一个集合中,我需要编写哪些简单的LINQ查询来获取两个表中最近的10个(基于time字段)记录?例如,此查询可能会返回A的7条记录和B条记录的3条记录。

2 个答案:

答案 0 :(得分:2)

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

SOURCE

答案 1 :(得分: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;
    }

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

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