public class ConsumableThreshold
{
public int ThresholdType { get; set; }
public int ManufacturerID { get; set; }
public int ModelID { get; set; }
public int ConsumableVariantID { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试检查共享属性的两个对象列表.我需要根据以前的比赛结果检查各种其他属性.
例如,如果ThresholdType匹配,那么我需要检查第二个属性,如果匹配,我需要检查ModelID.
我有这个查询,它有效地做了我想要的但是它有问题主要是我进一步向下钻,可读性将会降低.
var query= existingThresholds.Where(
e => defaultThresholds.Any(
d => d.ThresholdType == e.ThresholdType)).Where(
e => defaultThresholds.Any(
d => d.ManufacturerID == e.ManufacturerID)).ToList();
Run Code Online (Sandbox Code Playgroud)
我想这样做join但它不支持&&运营商.
var query2 = from e in existingThresholds
join d in defaultThresholdson
e.ThresholdType equals d.ThresholdType &&
e.ManufacturerID equals d.ManufacturerID
select e;
Run Code Online (Sandbox Code Playgroud)
有没有办法把它写成没有链接.Where()条件的查询?
当然 - 你只是想加入一个复合键,通常是用匿名类型完成的:
var query2 = from e in existingThresholds
join d in defaultThresholdson
on new { e.ThresholdType, e.ManufacturerID } equals
new { d.ThresholdType, d.ManufacturerID }
select e;
Run Code Online (Sandbox Code Playgroud)
(稍后忽略加入的一半有点奇怪,不可否认......)