- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在这样的查询中:
var q = from l in session.Linq<Letter>()
where
letterTypeSearch == null ? true :
(l.LetterType.ToString() == letterTypeSearch)
l.LetterType 是一个枚举。
更新在当前的 linq-to-nhibernate 中比较枚举似乎是不可能的。虽然 letterTypeSearch 是一个包含 LetterType
实例的字符串,该实例是 ToString()
ed 并且 LetterType
是从 int
继承的,有3种比较方式:
1- 在 String
中进行比较:这是不可能的,因为 l.LetterType.ToString()
会产生“(ArgumentOutOfRangeException):索引超出范围。必须为非负数并且小于集合的大小。参数名称:index, "error.
2- 比较 Enum
(LetterType
) 本身:这也是不可能的,因为 l.LetterType == LetterType.Internal
导致“(QueryException):NHibernate.Criterion.SimpleExpression 中的类型不匹配:LetterType 预期类型 System.Int32,实际类型 Faraconesh.EnterpriseAppUnits.OfficeAutomation.BusinessEntities.LetterType,"错误。
3- 在 Int32
中比较:然而不可能,因为 Convert.ToInt32(l.LetterType)
生成“(NotImplementedException): ToInt32 方法未实现。, "错误。
那么我如何比较 LINQ-to-NHibernate 中的枚举?这个问题是 LINQ-to-NHibernate 特有的还是所有 LINQ 用户都有这样的问题?
更新2这里是类,枚举和映射(smmarized):
public class Letter
{
private LetterType _letterType;
public LetterType LetterType
{
set
{
_letterType = value;
}//end
get
{
return _letterType;
}//end
}
}
=========
公共(public)枚举 LetterType{ 传入 = 0, 传出= 1, 内部 = 2,
=========
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false">
<class
name="Faraconesh.EnterpriseAppUnits.OfficeAutomation.BusinessEntities.Letter,Faraconesh.EnterpriseAppUnits.OfficeAutomation.BusinessEntities"
table="OfficeAutomation_Letter">
<property
name="LetterType" column="LetterType"
type="int" update="true" insert="true" access="property"
not-null="true"/>
</class>
</hibernate-mapping>
最佳答案
您已将枚举映射为 type="int"
,这会导致错误,可能是因为没有与 int 之间的隐式转换。如果删除 type 属性,枚举将映射到数据库中的一个 int 值,Linq 查询将起作用。
另请注意,在您的属性映射中,除了名称和类型之外的每个属性都是不必要的,因为它们指定了默认值。 “名称”是属性映射中唯一必需的属性,请参阅第 property 节在引用文档中。
<property name="LetterType" />
使用最新 (2.1.2GA) 版本的 NHibernate.Linq,可从 nhforge.org 上的 NHibernate Core 下载链接获得。 , 以下枚举查询按预期工作。
var q = from l in session.Linq<Letter>()
where l. LetterType == LetterType.A4
select l;
var result = q.ToList<Letter>();
LetterType? ltype = LetterType.A4;
q = from l in session.Linq<Letter>()
select l;
if (code != null) {
q = q.Where( l => l.LetterType == ltype.Value );
}
result = q.ToList<Letter>();
但是,如果 ltype 为 null,则最后一个查询的这种形式将不起作用,因为查询解析器仍将尝试使用 ltype.Value。
q = from l in session.Linq<Letter>()
where ltype != null && l => l.LetterType == ltype.Value
select l;
result = q.ToList<Letter>();
关于c# - 在 LINQ-to-NHibernate 或 LINQ 中比较枚举时有什么考虑吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2346537/
我一直认为使用“if”比捕获异常要好得多(就性能而言)。例如,这样做: User u = Users.getUser("Michael Jordan"); if(u!=null) System.
我正在尝试使用下一个格式将字符转换为日期。我有下一个数据框 i (我在最后添加了 dput() 版本的数据框): Date 1 Dec_28_2012_9:
考虑到 PHP 中的日期戳,我如何计算持续时间?我在日期之间使用的日期格式是“Y-m-d H:i:s”, 我的工作代码只能计算时间之间的持续时间而不考虑日期。 下面是我的代码: $assigned_t
我正在尝试解释 GLMM 中的自相关。我的响应变量是 bool 值,它表示一组 hive 的生命周期中是否存在 en 事件。我试图用一组描述每个巢状态的数值变量来预测此类事件的概率。因此,我在广义模型
我对如何解释 undefined variable 感到有点困惑(我不确定你现在是否可以)。我正在尝试使用以下代码底部附近的 if else 语句(已注释掉的行)。 这个想法是,如果请求歌曲的人不是与
Bjarne Stroustrup 的 The C++ Programming Language Fourth Edition 中的以下内容是什么意思? "Consider . (dot) suspe
我想要一个主元素,边 block 漂浮在它的右侧。我不知道边 block 的数量,也不知道它们的最终总高度。但是我的主要元素应该具有相同的高度(请参阅以下示例以更好地理解),而无需使用列。 (虚线部分
我在每个 TextView 上都有以下警告(来自 Lint),在我的 XML 中有一个 ID。 Consider making the text value selectable by specify
目前,我有 6 条曲线,以 6 种不同的颜色显示,如下所示。 这 6 条曲线实际上是由 一个相同实验 的 6 次试验生成的。这意味着,理想情况下它们应该是相同的曲线,但由于噪声和不同的试验参与者,它们
winner of a recent Wikipedia vandalism detection competition建议可以通过“检测考虑到 QWERTY 的随机键盘点击来改进检测键盘布局”。 示
多年来,我一直在编写 C 语言,主要是在嵌入式环境中,并且对指针有一个非常好的心智模型——我不必明确地考虑如何使用它们,我对指针算法 100% 感到满意,指针数组,指针指针等。 我写的 C++ 很少,
我正在使用 Boost.Date_time 来获取两个日期之间的时差。我希望代码在这些天也考虑夏令时的变化,并给我正确的时间间隔。 考虑这个例子。 2015 年 11 月 1 日,美国的 DST 将发
我有一个(人类)名字的向量,全部用大写字母表示: names <- c("FRIEDRICH SCHILLER", "FRANK O'HARA", "HANS-CHRISTIAN ANDERSEN")
我想呈现一个表单小部件。这是我要生成的原始 HTML: 使用这个: {{ form_row(form.email, { 'type' : 'email', 'attr' : { 'class' :
我正在开发一个 python 项目,它使用 pythonnet 和几个 C# dll 作为依赖项。 由于我不想将 dll 推送到 git 存储库,因此我调整了 .gitignore 文件。但是,现在
考虑到上午/下午,我想将字符串转换为 php 数据时间。 我想将 '03/06/2015 12:17 am' 转换为 php datatime。 我试过了, $myDateTime = DateTim
我想排除那些具有相同标题和同一年份的实例。 title votes ranking year 0 Wonderland 19 7.9 1931 1
例如对于一个 EditText,通常指定 android:inputType="numberDecimal"用于文本字段应该包含十进制数。但这假设“。”用作小数点分隔符,在某些国家/地区使用“,”代替
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 9 年前。 Improv
作为练习,我决定学习 Java 中的 lambda 表达式。我想重写我发现笨拙且冗长的旧代码。它检查命令行参数是否是(1)文件路径或(2)目录路径。在(1)场景中,它将命令行参数传递给方法。在 (2)
我是一名优秀的程序员,十分优秀!