score:3
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.
Edit:
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).
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"/>
score:0
I'm not completely sure about your queries but you could encapsulate the query logic in a stored procedure drag it with LINQ to SQL data context designer to use.
If you need the results independently and you really don't want the round trip to database server, I think you should fall back to SqlDataReader
and use NextResult
by calling a procedure that retrieves the whole data.
score:0
You may take the SQL code for each query and then send it to the DB in a single batch and finally iterating through the 14 result sets.
score:0
Nathan, you say the queries are not necessarily inter-dependents but are all related. What we need to know is if they are effectively 14 different result sets or it could be combined in one single result set. If the latter, why can't you create a view or stored procedure for this purpose? In some situations you need to use judgment to discern when to use other routes for a better performance and get the job done. Mixing approaches is not bad at all.
If you need 14 different result sets you could use Multiple Active Result Sets (MARS), by enabling it in the the connection string. This way they would be done in a single round trip.
score:0
If you rework your 'dodgy hack' it should run the queries simulateneously:
var data = from a in (from x in ... select x)
from b in (from x in ... select y)
from c in (from x in ... select z)
select new { A = a, B = b, C = c };
var result = data.First();
However, it will do a full join on all items, which is probably not what you would like to occur. Using IQueryable.Concat may work if the queries all return the same number of columns:
var data = (from x in ... select new { Part = 1, Val = x })
.AsQueryable() // may not be needed in your context
.Concat(
(from y in ... select new { Part = 2, Val = y })
.AsQueryable()
.Concat(
(from z in ... select new { Part = 3, Val = z }).AsQueryable()
)
);
foreach (result in data)
{
switch (result.Part)
{
// process each query's resultset
}
}
Still dodgy (and untested), but should only hit the DB once.
score:0
I'm not sure doing the queries off the Users table is really helping you at all.
Before writing the LINQ queries, try to think about how you would achieve your goal using basic SQL.
I'n not sure what you're trying to do exactly because you haven't provided much code, but, if you were to do this in a Stored Procedure, what would that look like, and what would it return?
I'm asking this because maybe the data structure you need just doesn't "fit" into a single query.
If it does, then you just need to rethink you're whole LINQ approach.
maybe you could provide more info on what Data you're actually trying to retrieve, and how?
cheers
score:0
Not sure if this helps, but Wondering why you wouldn't use a let keyword to enhance the query.
'let' allows you to run subqueries and store the results in a temp variable, which you can then further use in the master query
Source: stackoverflow.com
Related Articles
- How do I resolve multiple linq queries in one operation without using a base query?
- How to reuse a linq expression for 'Where' when using multiple source tables
- Using LINQ with a WHERE to ask for multiple possible results without redundant "||" commands
- Multiple CheckBox Selection Result using Linq without hardcoding
- Using LINQ SelectMany() to join multiple child tables without a navigation property
- how to generate where condition without multiple if condition using linq query
- Simplify the following code to a one-liner using Linq queries
- Is it possible to select multiple variables with LINQ without using keyword new?
- Convert string[] to int[] in one line of code using LINQ
- how to update the multiple rows at a time using linq to sql?
- Sequence contains no elements exception in linq without even using Single
- How to select multiple values from a Dictionary using Linq as simple as possible
- linq to sql join on multiple columns using lambda
- Using LINQ to find duplicates across multiple properties
- Using LINQ to group by multiple properties and sum
- Using Linq to do a Contains with multiple values
- using Linq with multiple where conditions
- how to group by multiple columns using linq
- How do I remove items from generic list, based on multiple conditions and using linq
- Is it possible to accelerate (dynamic) LINQ queries using GPU?
- If it's bad to use inline SQL, how does using LINQ to perform queries differ in practice?
- EF - Update multiple rows in database without using foreach loop
- Deleting multiple records with Entity Framework using a single LINQ query
- Why does LINQ to SQL translate GroupBy into multiple queries
- Select Multiple elements in a row using Linq
- Multiple SUM using LINQ
- Linq multiple where queries
- Multiple tables left join using Linq
- Does this LINQ code perform multiple lookups on the original data?
- Using expressions in the "Select" part of LINQ to entities queries
- Using Linq to group a list of objects that contains primitives data into a new grouped list of objects
- Appending .Where(..) to a Linq.IQueryable
- How to use Linq to query a table dependent on where a user belongs to another table
- Order list by Date with split between future as past dates
- Optimal LINQ query to get a random sub collection - Shuffle
- NotSupportedException when I'm using a method in the where clause
- LINQ grouping and ordering of top 5 rows into ViewModel List
- Building LINQ query
- Tally Values of Each Column
- Select a property from duplicate records in ef core linq
- Will Dispose be called first and cause a failure in case of deferred execution?
- Logic to decipher consecutive date ranges
- Find elements which aren't in other list
- Code review of this portion of Linq Filters
- Filling a view model from entity data, getting convertion error
- Left Outer Join Two Data Tables?
- Convert SQL to LINQ query to get max
- Find minimal and maximal date in array using LINQ?
- using Navigation property to get Data
- Linq: select parent based on sub collection