2014-04-03 2 views
2
public class ConsumableThreshold 
{ 
    public int ThresholdType { get; set; } 
    public int ManufacturerID { get; set; } 
    public int ModelID { get; set; } 
    public int ConsumableVariantID { get; set; } 
} 

공유 속성에 대한 개체 목록 두 개를 확인하려고합니다. 이전 경기의 결과에 따라 여러 가지 다른 속성을 확인해야합니다.여러 조건에서 목록 비교

예를 들어 ThresholdType이 일치하면 두 번째 속성을 확인해야하며 일치하는 경우 ModelID를 확인해야합니다.

나는이 쿼리를 사용하여 효과적으로 원하는 것을 수행하지만, 더 많은 것을 드릴 다운하면 가독성이 감소 될 것이므로 주로 문제가 있습니다.

var query= existingThresholds.Where(
    e => defaultThresholds.Any(
     d => d.ThresholdType == e.ThresholdType)).Where(
      e => defaultThresholds.Any(
       d => d.ManufacturerID == e.ManufacturerID)).ToList(); 

나는 join를 사용하여이 작업을 수행하고 싶어하지만 && 연산자를 지원하지 않습니다.

.Where() 조건을 연결하지 않고 쿼리로 작성하는 방법이 있습니까?

답변

6

물론 - 당신은 그냥 일반적으로 익명의 형태로 수행됩니다 복합 키에 가입하려는 :

var query2 = from e in existingThresholds 
      join d in defaultThresholdson 
      on new { e.ThresholdType, e.ManufacturerID } equals 
       new { d.ThresholdType, d.ManufacturerID } 
      select e; 

(그것은 틀림없이, 조인 나중에의 절반을 무시하는 약간 이상한. ..)

3

체인없이 쿼리로 작성하는 방법이 있습니까?

예, 내장 된 이름으로 모든 속성의 값을 비교 평등 체크 익명의 종류, 사용

var query2 = from e in existingThresholds 
      join d in defaultThresholds 
      on  new { e.ThresholdType , e.ManufacturerID } 
       equals new { d.ThresholdType , d.ManufacturerID } 
      select e;