这主要是为了看看我是否能找到解决这一限制的方法。
假设我有以下查询:
var query = (from a in db.Table
where a.CustomFieldId == FieldID && a.ListingId == listingID
select a.SomeTypeValue);我正在查询的表是针对类型可能不同的自定义字段设置的,因此它有几个列,但只使用适当的列来根据字段的选定类型存储值。
这张桌子看起来有点像这样:

我希望能够在不重写整个查询的情况下选择我选择的列。这个是可能的吗?
提前谢谢你,
发布于 2016-02-25 16:54:13
您的查询可以重写为使用“方法调用LINQ":
db.Table
.Where(a => a.CustomFieldId == FieldID && a.ListingId == listingID)
.Select(x => x.SomeType);您可以将查询拆分为Where和Select:
var result = whereQuery.Select(x => x.BoolValue);或
var result = whereQuery.Select(x => x.IntValue);您甚至可以将该逻辑封装到方法中:
IEnumerable<T> GetValues<T>() {
var query = db.Table
.Where(a => a.CustomFieldId == FieldID && a.ListingId == listingID);
if (typeof(T)==typeof(bool)) {
return query.Select(x => x.BoolColumn);
}
else if (typeof(T) == typeof(int)) {
return query.Select(x => x.IntColumn);
}
// other types here
}https://stackoverflow.com/questions/35633375
复制相似问题