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

标签 c# linq

我需要将下面的前后组合成一个结果,然后将其传递给数据 GridView 。我使用 before + after 因为我不知道如何组合结果。

        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();

最佳答案

你想要的 + 要么是 before.Concat(after) 要么是 before.Union(after),这取决于你想要的方式要处理的重复项(union 删除重复项;concatenation 不删除;例如 {1,2,2,3} concat {3,4} is {1 ,2,2,3,3,4},其中 {1,2,2,3} union {3,4}{1,2,3,4}(虽然顺序是否定义在union case中值得怀疑)

关于c# - 将 2 个查询的结果合并为 1 个结果,然后传递给 DGV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16176577/

相关文章:

c# - 当它只有一位时如何获得两位小数

c# - linq 问题 : querying nested collections

c# - SelectMany() 无法推断类型参数——为什么不呢?

linq - 编译的 LINQ 查询 - NHibernate

c# - Visual Studio 2012 命令行编译器

c# - 并非所有代码路径都返回值: Unity

c# - 将枚举绑定(bind)到 LINQ 和 SelectListItem

c# - 在 DataTable 上构建表达式,设计建议?

c# - 声明一个类 protected 会自动使其成员受到保护吗?

C# 定时器和线程安全