score:1
I've used Contains
to run a bulk filter similar to where-in. I setup a rough approximation of your scenario. The single select queries actually ran quicker than Contains
did. I recommend running a similar test on your end with the DB tied in to see how your results wind up. Ideally see how it scales too. I'm running .NET 4.0 in visual studio 2012. I jammed in ToList()
calls to push past potential lazy loading problems.
public class TrainingPlan
{
public int WorkgroupId { get; set; }
public int AreaId { get; set; }
public TrainingPlan(int workGroupId, int areaId)
{
WorkgroupId = workGroupId;
AreaId = areaId;
}
}
public class TrainingPlanComparer : IEqualityComparer<TrainingPlan>
{
public bool Equals(TrainingPlan x, TrainingPlan y)
{
//Check whether the compared objects reference the same data.
if (x.WorkgroupId == y.WorkgroupId && x.AreaId == y.AreaId)
return true;
return false;
}
public int GetHashCode(TrainingPlan trainingPlan)
{
if (ReferenceEquals(trainingPlan, null))
return 0;
int wgHash = trainingPlan.WorkgroupId.GetHashCode();
int aHash = trainingPlan.AreaId.GetHashCode();
return wgHash ^ aHash;
}
}
internal class Class1
{
private static void Main()
{
var plans = new List<TrainingPlan>
{
new TrainingPlan(1, 2),
new TrainingPlan(1, 3),
new TrainingPlan(2, 1),
new TrainingPlan(2, 2)
};
var filter = new List<TrainingPlan>
{
new TrainingPlan(1, 2),
new TrainingPlan(1, 3),
};
Stopwatch resultTimer1 = new Stopwatch();
resultTimer1.Start();
var results = plans.Where(plan => filter.Contains(plan, new TrainingPlanComparer())).ToList();
resultTimer1.Stop();
Console.WriteLine("Elapsed Time for filtered result {0}", resultTimer1.Elapsed);
Console.WriteLine("Result count: {0}",results.Count());
foreach (var item in results)
{
Console.WriteLine("WorkGroup: {0}, Area: {1}",item.WorkgroupId, item.AreaId);
}
resultTimer1.Reset();
resultTimer1.Start();
var result1 = plans.Where(p => p.AreaId == filter[0].AreaId && p.WorkgroupId == filter[0].WorkgroupId).ToList();
var result2 = plans.Where(p => p.AreaId == filter[1].AreaId && p.WorkgroupId == filter[1].WorkgroupId).ToList();
resultTimer1.Stop();
Console.WriteLine("Elapsed time for single query result: {0}",resultTimer1.Elapsed);//single query is faster
Console.ReadLine();
}
}
score:0
It seems to me that using Intersect() may get this done the way that you want. But, I don't have an environment set up to test this myself.
var q = (from tp in context.TPM_TRAININGPLAN.Include("TPM_TRAININGPLANSOLUTIONS")
where pid == tp.PROJECTID
select tp)
.Intersect
(from tp in context.TPM_TRAININGPLAN.Include("TPM_TRAININGPLANSOLUTIONS")
where plans.Any(p => p.AreaID == tp.AREAID)
select tp)
.Intersect
(from tp in context.TPM_TRAININGPLAN.Include("TPM_TRAININGPLANSOLUTIONS")
where plans.Any(p => p.WorkgroupId == tp.WORKGROUPID)
select tp);
My only concern might be that Intersect could cause it to load more records in memory than you would want, but I'm unable to test to confirm if that's the case.
Source: stackoverflow.com
Related Articles
- LINQ query to select rows matching an array of pairs
- Linq Query returning rows matching all words in array
- Linq Select All Items Matching Array
- Which LINQ query to select rows from 1 table that are not in another table
- EF Core Linq Query to Select Rows That Match From a List of Possibilities
- Select fields in a matching array and parent with C# Linq
- How to Select top (5) contributors group by Business type code in c# linq query
- How to write a LINQ query to select from a collection with given set of matching IDs
- How to write aggregate query in LINQ reusing part of the select code
- Linq query to select rows where a column is a max value
- LINQ to SQL: select array of arrays of integers in one query
- LINQ query to to iterate through list of lists but select an item at the upper level matching given filter conditions
- Linq query to select all records that have sub records that match all values in an array of values?
- Linq query to take the value from Array with matching sub string value
- object must implement IConvertible error in linq query to select two rows
- LINQ query to select top five
- Linq code to select one item
- How to Select Min and Max date values in Linq Query
- LINQ select one field from list of DTO objects to array
- Can I select multiple objects in a Linq query
- Linq UNION query to select two elements
- How to write linq query to match SQL like select top 100 * from tab?
- Linq query with Array in where clause?
- Linq to Select Parent Objects Where Child Objects Have a Matching Child Object
- C# LINQ select from where value is not contained in array / list
- Take the first five elements and the last five elements from an array by one query using LINQ
- LINQ query to perform a projection, skipping or wrapping exceptions where source throws on IEnumerable.GetNext()
- How to select top N rows in a Linq GroupBy
- Select distinct rows from datatable in Linq
- LINQ select query with Anonymous type and user Defined type
- Get the parent node on the basis of single child element with specific attribute
- Linq Query with join on subquery
- Error 1 'Form1.CountWordsInstances(string, string)': not all code paths return a value
- How to select elements from a table that are present in a junction table with LINQ - Entity Framework
- Is there a way to call built-in sql function in Linq to Entities
- Selecting the newest entry of each day
- Problem using string.IsnullorEmpty in linq query
- Linq to EF - populate a (fairly simple) hierarchical object from EDM C#
- LINQ: Filtering items List by some condition and do something with item by condition
- C# Linq Char arrays Except() - Weird behavior
- Use fLinq to persist?
- Linq with Different Where clauses and GroupBy
- Search a multidimensional array object's content using linq
- SQL duplicate rows query in Linq
- .NetCore Querying - Joining Many-to-Many Tables And Returning Result and Including a Key With Array of Strings
- LINQ NOT Field LIKE String - String LIKE Field
- build poll system with Repeater control in asp.net
- Does order of conditions under where clause in a LINQ query matter
- LINQ to Entities - Get results based on join, conditions and sorting
- How to get the operator from a BinaryExpression