是否可以在SubSonic查询中应用WHERE子句?
例如,我得到一个基于id ...
的单曲db.Single<Storage>(id);
但是如何基于简单的WHERE子句获得单个?
db.Single<Storage>(WHERE columnname == "value");
答案 0 :(得分:1)
这是可能的:
// Will return a Storage instance with property IsNew = true, if record does not exist
// since an object created with new never can be null
var storage1 = new Storage(1); // id = 1
var storage1 = new Storage(Storag.Columns.ColumnName, "value");
// Will return 0 if record not found (subsonic3 only)
var storage3 = (from s in Storage
where s.ColumnName == "value"
select s).SingleOrDefault();
// Will throw an exception if record not found (subsonic3 only)
var storage3 = (from s in Storage
where s.ColumnName == "value"
select s).Single();
由于db是一个部分类,你可以扩展它。只需在同一命名空间内创建一个新文件(但解决方案中的另一个文件夹)。这适用于亚音速2,但我认为它类似于亚音速3。
public static partial class DB
{
public static T Single<T>(String columName, Object columnValue) where T: RecordBase<T>, new()
{
return Select().From<T>()
.Where(columnName).IsEqualTo(columnValue)
.ExecuteSingle<T>();
}
}
答案 1 :(得分:1)
感谢上述情况,这是一个帮助,最终我将其简化为以下内容......
db.Single<Storage>(s => s.ColumnName == "value");