将2个查询的结果合并为1个结果,然后传递给DGV

时间:2013-04-23 18:28:16

标签: c# linq

我需要在下面和之后组合成一个结果然后将它传递给datagridview。因为我不知道如何结合结果,所以我之前和之后使用过。

        private void textBox6_Leave(object sender, EventArgs e)
    {
        DataClasses3DataContext db = new DataClasses3DataContext();

        int matchedAdd = (from c in db.GetTable<prop>()
                          where c.HOUSE_NO.Contains(textBox1.Text) && c.Direction.Contains(textBox2.Text) && c.street.Contains(textBox3.Text) && c.SUFF.Contains(textBox4.Text)
                          select c.ID).SingleOrDefault();

        var before = (from c in db.GetTable<prop>()
                      where c.ID < matchedAdd
                      orderby c.PARCEL descending
                      select c).Take(6);

        var after = (from c in db.GetTable<prop>()
                     where c.ID > matchedAdd
                     orderby c.PARCEL
                     select c).Take(6);

        var endResult = before + after;

        dgvBRT.DataSource = endResult;
        dgvBRT.Databind();

2 个答案:

答案 0 :(得分:5)

您想要的+before.Concat(after)before.Union(after),具体取决于您希望如何处理重复项(union会删除重复项;连接不会;例如{{1} } concat {1,2,2,3}{3,4},其中-{1,2,2,3,3,4} union {1,2,2,3}{3,4}(尽管在联合案例中是否定义了订单仍有疑问)< / p>

答案 1 :(得分:1)

你可以使用union-operator:

var endresult = before.Union(after);