是否可以将两个 IQueryable 添加在一起?

发布于 2024-09-14 11:44:08 字数 401 浏览 5 评论 0原文

我一直在 IQueryable<> 上使用 Union (具有相同类型)尝试从两个集合生成一个集合,但它不起作用。我相信我对 IQueryable 成员的理解是错误的。

我想我可以使用 union 方法(在 foreach 内)构建一个 SQL 语句,该语句将执行以下内容:

SELECT * FROM MYTABLE WHERE FK = 1
UNION
SELECT * FROM MYTABLE WHERE FK = 9
UNION
SELECT * FROM MYTABLE WHERE FK = 15

以前我认为必须有一个可用的方法,它可以对两个 IQueryable 产生与旧的最喜欢的 ArrayList 的 AddRange 相同的效果,这确实是我在这里想要实现的目标的基础。

I've been using Union on IQueryable<> (with the same type) to try to get one collection produced from two collections, but it is not working. I believe my understanding is at fault with regards to the IQueryable members.

I thought I could use the union method to build (within a foreach) a SQL statement which would execute something like:

SELECT * FROM MYTABLE WHERE FK = 1
UNION
SELECT * FROM MYTABLE WHERE FK = 9
UNION
SELECT * FROM MYTABLE WHERE FK = 15

Previously I had thought that there must be an method available which produces the same effect on two IQueryable's as the old favourite ArrayList's AddRange, which is really the basic of what I'm trying to achieve here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

复古式 2024-09-21 11:44:09

实际上,这完全符合预期:(

var q1 = from c in db.Customers where c.CustomerID.StartsWith("A") select c;
var q2 = from c in db.Customers where c.CustomerID.StartsWith("B") select c;
var q3 = from c in db.Customers where c.CustomerID.StartsWith("C") select c;

var q4 = q1.Union(q2).Union(q3);

我正在使用 Northwind 数据库)。它生成一个 SQL 语句来联合三个查询。

更新:我想我误解了你所说的“循环”的意思。这工作正常:)

static void Main(string[] args)
{
    var db = new NorthwindDataContext();
    IQueryable<Customer> query = from c in db.Customers 
                                 where c.CustomerID.StartsWith("A") 
                                 select c;

    foreach (var ch in "EIOV")
    {
        var chh = ch.ToString();  // separate variable so closure work right

        query = query.Union(from c in db.Customers 
                            where c.CustomerID.StartsWith(chh) 
                            select c);
    }

    foreach (var c in query)
    {
        Console.WriteLine(c.CompanyName);
    }
}

Actually, this works exactly as expected:

var q1 = from c in db.Customers where c.CustomerID.StartsWith("A") select c;
var q2 = from c in db.Customers where c.CustomerID.StartsWith("B") select c;
var q3 = from c in db.Customers where c.CustomerID.StartsWith("C") select c;

var q4 = q1.Union(q2).Union(q3);

(I'm using the Northwind database). It generates one SQL statement which unions the three queries.

UPDATE: I think I misunderstood what you meant by "in a loop". This works correctly:)

static void Main(string[] args)
{
    var db = new NorthwindDataContext();
    IQueryable<Customer> query = from c in db.Customers 
                                 where c.CustomerID.StartsWith("A") 
                                 select c;

    foreach (var ch in "EIOV")
    {
        var chh = ch.ToString();  // separate variable so closure work right

        query = query.Union(from c in db.Customers 
                            where c.CustomerID.StartsWith(chh) 
                            select c);
    }

    foreach (var c in query)
    {
        Console.WriteLine(c.CompanyName);
    }
}
暗恋未遂 2024-09-21 11:44:09

我终于通过使用 List<> 解决了这个问题只需调用 AddRange() 即可。列表完成后,我调用 list.AsQueryable() 来获取 IQueryable 集合。

这是最好的方法吗?
这将为列表中的每个添加创建一个 SQL 调用 - 是否没有办法创建正确的 UNION 语句?

I finally solved this by using a List<> and simply calling AddRange(). Once the list was completed I then called list.AsQueryable() to get the IQueryable collection back out.

Is this the best approach?
This will create a SQL call for each addition to the list - is there no way to create a correct UNION statement?

七七 2024-09-21 11:44:09

IQueryable.Concat() 会是你在找什么?

Would IQueryable.Concat() be what you looking for?

清引 2024-09-21 11:44:09

您可以尝试这样的操作:

var qry = 
new [] {
    "one", "two", "three", "four"
}
.Select(z=>MyTable.Where(x=>x.Number==z))
.Aggregate((a,x)=>a.Union(x))
.ToList();

它的作用是创建一个 IQueryable,然后您可以将其具体化(请参阅 ToList())以生成单个服务器端语句。

You might try something like this:

var qry = 
new [] {
    "one", "two", "three", "four"
}
.Select(z=>MyTable.Where(x=>x.Number==z))
.Aggregate((a,x)=>a.Union(x))
.ToList();

What this does is create a single IQueryable that you can then materialize (see the ToList()) to generate a single server-side statement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文