3 가지 상황에서 결과 집합이 95 % 동일한 상황이 있습니다. 5 % 차이는 주어진 변수에 따라 다르므로 나머지 5 %의 필드를 채 웁니다 (또는 포함하지 않음). 현재기존 IQueryable에 다중 선택을 추가하는 방법
public class MyResults {
public string PropertyA { get; set; }
public string PropertyB { get; set; }
public string PropertyC { get; set; }
public string PropertyD { get; set; }
public string PropertyE { get; set; }
}
나는 나는 다음과 같은 한 결과 구축하는 방법이 있습니다 :
public List<MyResults> GetMyResults(int someParameter) {
IQueryable<MyResults> query;
if (someParameter == "A") {
query = entities.Select(x => new MyResults {
PropertyA = x.PropertyA, // Common
PropertyB = x.PropertyB, // Common
PropertyC = x.PropertyC, // Different
};
} else if (someParameter == "B") {
query = entities.Select(x => new MyResults {
PropertyA = x.PropertyA, // Common
PropertyB = x.PropertyB, // Common
PropertyD = x.PropertyD, // Different
};
} else {
query = entities.Select(x => new MyResults {
PropertyA = x.PropertyA, // Common
PropertyB = x.PropertyB, // Common
PropertyE = x.PropertyE, // Different
};
}
return query.ToList();
}
을 간단한 예를 들어
, 여기에 돌려지고 결과 개체입니다 이것은 입니다. 원하는 작업 :public List<MyResults> GetMyResults(int someParameter) {
IQueryable<MyResults> query = entities.Select(x => new MyResults {
PropertyA = x.PropertyA, // Common
PropertyB = x.PropertyB, // Common
PropertyC = x.PropertyC, // Common
};
if (someParameter == "A") {
query = entities.Select(x => new MyResults {
PropertyC = x.PropertyC // Different
});
} else if (someParameter == "B") {
query = entities.Select(x => new MyResults {
PropertyD = x.PropertyD // Different
});
} else {
query = entities.Select(x => new MyResults {
PropertyE = x.PropertyE // Different
});
}
return query.ToList();
}
이 방법을 사용하면 모든 결과의 일관된 필드가 동일 해지고 다른 필드 만 추가하면됩니다.
이것이 가능합니까? 다음과 같이
이상한 냄새가납니다. 왜 항상'PropertyC','PropertyD','PropertyE'를 설정할 수 없습니까? –