如何解决在一个操作中的多个LINQ查询,而无需使用基础查询? [英] How do I resolve multiple linq queries in one operation without using a base query?

查看:187
本文介绍了如何解决在一个操作中的多个LINQ查询,而无需使用基础查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有14 LINQ查询的方法来解决。他们都没有一个基本查询,我可以从作为子查询挂,然后存储结果为匿名类型实例的属性。

I have 14 LINQ queries to resolve in one method. None of them have a base query that I could hang them from as subqueries and then store the results as properties of an anonymous type instance.

而不是进行到14个独立的呼叫数据库中,我怎么能保证他们都被称为在同一操作?

Rather than making 14 separate calls to the database, how can I ensure that they are all called in the same operation?

更新

我结束了使用该做的伎俩一个狡猾的黑客。我知道,总是会有在user表中至少一个用户,所以我最终使用:

I ended up using a dodgy hack which did the trick. I know that there will ALWAYS be at least one user in the user table, so I ended up using:

var data = (from tmp in DataContext.Users
            select new {
                Property1 = (from...),
                Property2 = (from...),
                PropertyN = (from...),
            }).First();



更新2

已经暗示了答案,这样做可能会造成MARS(多个活动结果集)错误之一。这些是什么错误(我从来没有见过一个),为什么他们发生,是有什么不对这一行质疑的整个前提?

It has been implied in one of the answers that doing this may create MARS (Multiple Active Result Set) errors. What are these errors (I have never seen one), why do they occur and is there anything wrong the whole premise of this line of questioning? i.e. Am I flawed in my assertion that asking the database to return everything in one go is going to be quicker/more efficient than doing fourteen wholly separate LINQ queries?

更新3

我觉得我的做法是多余的,从务实的角度来看,应该有不同的处理,无论是通过存储过程或某种延迟/混合的方法。

I think my approach is redundant and from a pragmatic perspective, should be handled differently, either via a stored procedure or some sort of delayed/mixed approach.

推荐答案

您还在做14单独调用的到数据库通过每个子查询,你只是做。在当你的数据对象被称为一个实例

You're still making 14 seperate call's to the database through each of your sub queries, your just doing it at a single instance when your data object is called.

编辑:

当你出现火星错误多个开放数据读者的连接。默认情况下使用SQL 2005这被关闭。在您的查询,而所有的SQL在一个被传递打你还是回到14的DataReader。 DataReader的需要就此exclusivly除非你告诉的连接,允许多个活动结果集(MARS)。

The MARS error occurs when you have multiple open data readers for a connection. By default with SQl 2005 this is turned off. in your query, while all the SQL is being passed in one hit your still returning 14 datareaders. The datareader takes that connection exclusivly unless you tell the connection to allow multiple active result sets (MARS).

要解决这一点,你要么需要每个子查询预装入清单和运行子查询了该列表,或者你需要设置连接字符串的MutlipleActiveResultSet属性=真。

To resolve this you either need to preload each of your sub queries into a list and run your subqueries off of that list, or you need to set the MutlipleActiveResultSet attribute of the connection string = true.

<add name="Name" connectionString="Data Source=<server>;Initial Catalog=<database>;Integrated Security=True;MultipleActiveResultSets=true" providerName="System.Data.SqlClient"/>

这篇关于如何解决在一个操作中的多个LINQ查询,而无需使用基础查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆