从v2.2.1版起,NBear开始支持强类型的实体查询语法。例如,我们可以以如下的语法查询需要的数据:
LocalUser[] users = gateway.Select(_Entity.LocalUser.Id 5 | _Entity.LocalUser.LoginId == "teddy", _Entity.LocalUser._OrderBy.Id_Desc & _Entity.LocalUser._OrderBy.LoginId);
从v2.2.1版起,NBear开始支持强类型的实体查询语法。例如,我们可以以如下的语法查询需要的数据:
LocalUser[] users = gateway.Select<LocalUser>(_Entity.LocalUser.Id > 5 | _Entity.LocalUser.LoginId == "teddy", _Entity.LocalUser.Id.Desc & _Entity.LocalUser.LoginId.Asc);
注意黑体部分,Select函数的两个参数分别为两个强类型表达式,一个是一组查询条件,另一个是排序条件。
以上代码等价于:
LocalUser[] users = gateway.Select<LocalUser>("[Id] > @Id or [LoginId] = @LoginId", "[Id] desc, [LoginId]", new object[] { 5, "teddy" });
我们可以看到,采用第一种语法的好处有:
· 强类型,对于代码错误拥有编译器错误检查;
· 更直观;
· 独立与数据库特定的SQL语法,从而隔离了业务层和特定数据库的耦合;
使用Entity Configurator生成强类型实体查询代码
要让程序能够访问到_Entity命名空间下的这些强类型查询对象,需要先使用Entity Configurator生成强类型实体查询代码。只需在Entity Configurator中载入实体程序集,设置要对应的元数据,点击工具的Code->Generate EntityQuery Code菜单项,再将生成的代码复制到需要的任意程序集中,生成的代码及包含了所有实体的强类型查询对象。
可用操作符
NBear的像类型实体查询语法支持的操作符有:&(与),|(或),==(等于),!=(不等于),>(大于),<(小与),>=(大于等于),<=(小于等于),!(否),以及.Like()注意,Like是一个函数,可以以_Entity.User.LoginId.Like("teddy")这样的方式使用。
对于OrderBy部分,唯一的可用操作符是&,即表示组合多个排序条件。
代码示例
下面的代码是使用Entity Configurator工具为使用Entity Configurator设置实体元数据、生成数据库创建脚本一文中所示的实体生成的强类型实体查询代码:
namespace _Entity

{
public class AgentUser

{
public static PropertyItem LoginId = new PropertyItem("LoginId", "[", "]", "@");
public static PropertyItem Id = new PropertyItem("Id", "[", "]", "@");
public static PropertyItem Name = new PropertyItem("Name", "[", "]", "@");
public static PropertyItem PrivilegeOwnerId = new PropertyItem("PrivilegeOwnerId", "[", "]", "@");
}

public class GhostUser

{
public static PropertyItem Id = new PropertyItem("Id", "[", "]", "@");
public static PropertyItem Name = new PropertyItem("Name", "[", "]", "@");
public static PropertyItem PrivilegeOwnerId = new PropertyItem("PrivilegeOwnerId", "[", "]", "@");
}

public class IdentableEntity

{
public static PropertyItem Id = new PropertyItem("Id", "[", "]", "@");
public static PropertyItem Name = new PropertyItem("Name", "[", "]", "@");
}

public class IdFactory

{
public static PropertyItem NextId = new PropertyItem("NextId", "[", "]", "@");
}

public class LocalUser

{
public static PropertyItem Password = new PropertyItem("Password", "[", "]", "@");
public static PropertyItem LoginId = new PropertyItem("LoginId", "[", "]", "@");
public static PropertyItem Id = new PropertyItem("Id", "[", "]", "@");
public static PropertyItem Name = new PropertyItem("Name", "[", "]", "@");
public static PropertyItem PrivilegeOwnerId = new PropertyItem("PrivilegeOwnerId", "[", "]", "@");
}

public class Loginable

{
public static PropertyItem LoginId = new PropertyItem("LoginId", "[", "]", "@");
}

public class PasswordLoginable

{
public static PropertyItem Password = new PropertyItem("Password", "[", "]", "@");
public static PropertyItem LoginId = new PropertyItem("LoginId", "[", "]", "@");
}

public class PrivilegeAssignable

{
public static PropertyItem PrivilegeOwnerId = new PropertyItem("PrivilegeOwnerId", "[", "]", "@");
}

public class User

{
public static PropertyItem Id = new PropertyItem("Id", "[", "]", "@");
public static PropertyItem Name = new PropertyItem("Name", "[", "]", "@");
public static PropertyItem PrivilegeOwnerId = new PropertyItem("PrivilegeOwnerId", "[", "]", "@");
}

public class UserGroup

{
public static PropertyItem Comment = new PropertyItem("Comment", "[", "]", "@");
public static PropertyItem Id = new PropertyItem("Id", "[", "]", "@");
public static PropertyItem Name = new PropertyItem("Name", "[", "]", "@");
public static PropertyItem PrivilegeOwnerId = new PropertyItem("PrivilegeOwnerId", "[", "]", "@");
}

}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?