亚音速单个 WHERE 子句

发布于 2024-09-25 12:41:07 字数 232 浏览 0 评论 0原文

是否可以在 SubSonic 查询上应用 WHERE 子句?

例如,我根据 id 获得单曲...

db.Single<Storage>(id);

但是如何根据简单的 WHERE 子句获得单曲?

db.Single<Storage>(WHERE columnname == "value");

Is it possible to apply a WHERE clause on a SubSonic query?

For example, I get get a single based on id...

db.Single<Storage>(id);

But how can I get a single based on a simple WHERE clause?

db.Single<Storage>(WHERE columnname == "value");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑咖 2024-10-02 12:41:07

这是可能的:

// 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>();
    }
}

that's possible:

// 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();   

Since db is a partial class you can extend it. Just create a new File within the same namespace (but another folder in your solution). This applies to subsonic 2 but will be similar to subsonic 3, I think.

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>();
    }
}
风渺 2024-10-02 12:41:07

感谢以上内容,这是一个帮助,最终我将其简化为以下......

        db.Single<Storage>(s => s.ColumnName == "value");

Thanks for the above, this was a help and eventually I simplified this to the below...

        db.Single<Storage>(s => s.ColumnName == "value");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文