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();
}
このように、ALL結果の一貫性のあるフィールドは同じで、異なるものを追加するだけです。
これは可能ですか?次のように
これは奇妙なにおいをします。なぜ、あなたは常に 'PropertyC'、' PropertyD'、そして 'PropertyE'を設定できませんか? –