是否可以将两个 IQueryable 添加在一起?
我一直在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
实际上,这完全符合预期:(
我正在使用 Northwind 数据库)。它生成一个 SQL 语句来联合三个查询。
更新:我想我误解了你所说的“循环”的意思。这工作正常:)
Actually, this works exactly as expected:
(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:)
我终于通过使用 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?
IQueryable.Concat() 会是你在找什么?
Would IQueryable.Concat() be what you looking for?
您可以尝试这样的操作:
它的作用是创建一个 IQueryable,然后您可以将其具体化(请参阅 ToList())以生成单个服务器端语句。
You might try something like this:
What this does is create a single IQueryable that you can then materialize (see the ToList()) to generate a single server-side statement.