public class ConsumableThreshold
{
public int ThresholdType { get; set; }
public int ManufacturerID { get; set; }
public int ModelID { get; set; }
public int ConsumableVariantID { get; set; }
}
I'm attempting to check two lists of objects for shared properties. I will need to check various other properties depending on the results of previous matches.
For example if the ThresholdType matches, I then need to check a second property and if that matches I need to check the ModelID.
I have this query, which effectively does what I want but there are problems with it mainly that further down I drill the more the readability is going to be reduced.
var query= existingThresholds.Where(
e => defaultThresholds.Any(
d => d.ThresholdType == e.ThresholdType)).Where(
e => defaultThresholds.Any(
d => d.ManufacturerID == e.ManufacturerID)).ToList();
I wanted to do this using join
but it does not support the &&
operator.
var query2 = from e in existingThresholds
join d in defaultThresholdson
e.ThresholdType equals d.ThresholdType &&
e.ManufacturerID equals d.ManufacturerID
select e;
Is there a way to write this as a query without chaining .Where()
conditions?
Sure - you're just trying to join on a compound key, which is usually accomplished with an anonymous type:
var query2 = from e in existingThresholds
join d in defaultThresholdson
on new { e.ThresholdType, e.ManufacturerID } equals
new { d.ThresholdType, d.ManufacturerID }
select e;
(It's slightly odd to ignore one half of the join later on, admittedly...)
Is there a way to write this as a query without chaining .Where() conditions?
Yes, use an anonymous type, which has a built-in equality check that compares the values of all properties by name:
var query2 = from e in existingThresholds
join d in defaultThresholds
on new { e.ThresholdType , e.ManufacturerID }
equals new { d.ThresholdType , d.ManufacturerID }
select e;
public class ConsumableThreshold
{
public int ThresholdType { get; set; }
public int ManufacturerID { get; set; }
public int ModelID { get; set; }
public int ConsumableVariantID { get; set; }
}
I'm attempting to check two lists of objects for shared properties. I will need to check various other properties depending on the results of previous matches.
For example if the ThresholdType matches, I then need to check a second property and if that matches I need to check the ModelID.
I have this query, which effectively does what I want but there are problems with it mainly that further down I drill the more the readability is going to be reduced.
var query= existingThresholds.Where(
e => defaultThresholds.Any(
d => d.ThresholdType == e.ThresholdType)).Where(
e => defaultThresholds.Any(
d => d.ManufacturerID == e.ManufacturerID)).ToList();
I wanted to do this using join
but it does not support the &&
operator.
var query2 = from e in existingThresholds
join d in defaultThresholdson
e.ThresholdType equals d.ThresholdType &&
e.ManufacturerID equals d.ManufacturerID
select e;
Is there a way to write this as a query without chaining .Where()
conditions?
Sure - you're just trying to join on a compound key, which is usually accomplished with an anonymous type:
var query2 = from e in existingThresholds
join d in defaultThresholdson
on new { e.ThresholdType, e.ManufacturerID } equals
new { d.ThresholdType, d.ManufacturerID }
select e;
(It's slightly odd to ignore one half of the join later on, admittedly...)
Is there a way to write this as a query without chaining .Where() conditions?
Yes, use an anonymous type, which has a built-in equality check that compares the values of all properties by name:
var query2 = from e in existingThresholds
join d in defaultThresholds
on new { e.ThresholdType , e.ManufacturerID }
equals new { d.ThresholdType , d.ManufacturerID }
select e;
0 commentaires:
Enregistrer un commentaire