- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个使用 Entity Framework 的 ASP.NET MVC 项目。
我需要将从数据库中提取的值投影到 PropertyValue
中类型,如下所示:
public class PropertyValue {
public string StringValue { get; set; }
public bool? BoolValue { get; set; }
public int? IntValue { get; set; }
}
大多数时候,这很容易。要过滤所有“First Name”=“John”且“Is Archived”= true 的用户,我可以这样做:
usersQuery
.Where(u =>
new PropertyValue {
StringValue = u.FirstName,
BoolValue = null,
IntValue = null
}.StringValue == "John")
.Where(u =>
new PropertyValue {
StringValue = null,
BoolValue = u.IsArchived,
IntValue = null
}.BoolValue == true);
显然这是一个看起来很荒谬的查询,但我正在根据用户输入逐个构建这些查询。这些查询需要可组合,这就是为什么我必须显式设置 PropertyValue
的所有未使用属性的原因为空,我必须以相同的顺序设置它们。如果我不这样做,我会从 Entity Framework 收到一条错误消息,指出 PropertyValue 存在于查询中两个结构上不兼容的初始化中。
System.NotSupportedException: The type 'UMS.Utilities.PropertyValue' appears in two structurally incompatible initializations within a single LINQ to Entities query. A type can be initialized in two places in the same query, but only if the same properties are set in both places and those properties are set in the same order.
这不是问题,我只需确保所有属性都明确设置为 null。当我必须向我的 PropertyValue
添加另一个属性时,问题就出现了。类型,以便能够检索 ID 列表(在我的例子中,检索用户的所有选定角色,即“管理员”、“监护人”、“教师”、“学生”)
我修改了我的 PropertyValue
能够存储 Guid 列表的类:
public class PropertyValue {
public string StringValue { get; set; }
public bool? BoolValue { get; set; }
public int? IntValue { get; set; }
public IEnumerable<Guid> GuidValues { get; set; }
}
我现在可以像这样查询用户角色:
usersQuery
.Select(u => new PropertyValue {
StringValue = null,
BoolValue = null,
IntValue = null,
GuidValues = u.UserRoles.Select(ur => ur.Role_ID)
});
效果很好。提取名字现在看起来像这样:
usersQuery
.Select(u => new PropertyValue {
StringValue = u.FirstName,
BoolValue = null,
IntValue = null,
GuidValues = null
});
但是我得到以下错误:
System.NotSupportedException: Unable to create a null constant value of type 'System.Collections.Generic.IEnumerable`1[[System.Guid, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'. Only entity types, enumeration types or primitive types are supported in this context.
所以我无法在 EF 中创建 null 枚举。但我不能只忽略属性的初始化,否则我会得到结构不兼容的错误。我试过做 GuidValues = Enumerable.Empty<Guid>()
相反,但我也不能在 EF 上下文中这样做。
如何解决此问题并在 EF 投影中创建 null 或空可枚举?
最佳答案
我不太确定用例。但总的来说,我同意这是令人讨厌的 EF 限制,尤其是结构上不兼容的初始化 要求背后的原因,这迫使我们使用我将向您推荐的丑陋解决方法。
很快,除了不包括分配到投影中之外,没有办法解决null enumerable分配。接下来是尝试解决结构上不兼容的初始化问题。除了 Concat
/Union
场景外,它在大多数情况下都有效。
首先定义几个子类:
public class StringPropertyValue : PropertyValue { }
public class BoolPropertyValue : PropertyValue { }
public class IntPropertyValue : PropertyValue { }
public class GuidsPropertyValue : PropertyValue { }
名称并不重要,因为没有对这些类施加特殊限制,唯一重要的是它们不同。我们将使用这些类代替 PropertyValue
进行投影。
然后我们需要另一个像这样的简单类:
public class Property
{
public PropertyValue Value { get; set; }
}
需要解决另一个 EF 限制 - 不支持的 casts。为了投影 PropertyValue
,而不是不受支持
(PropertyValue)new StringPropertyValue { StringValue = .. }
我们将使用丑陋但受支持的方式进行转换
new Property { Value = new StringPropertyValue { StringValue = ... } }.Value
就是这样。
以下是您的示例的等效工作版本:
(1)
usersQuery
.Where(u => new Property { Value = new StringPropertyValue {
StringValue = u.FirstName
}}.Value.StringValue == "John")
.Where(u => new Property { Value = new BoolPropertyValue {
BoolValue = u.IsArchived
}}.Value.BoolValue == true);
(2)
usersQuery
.Select(u => new Property { Value = new GuidsPropertyValue {
GuidValues = u.UserRoles.Select(ur => ur.Role_ID)
}}.Value);
(3)
usersQuery
.Select(u => new Property { Value = new StringPropertyValue {
StringValue = u.FirstName
}}.Value);
关于c# - 使用 Linq to Entities 查询创建 null ienumerable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37106147/
任何人都可以向我解释 IEnumerable 和 IEnumerator 之间的区别是什么, 以及如何使用它们? 谢谢!!! 最佳答案 通常,一个 IEnumerable是可以枚举的对象,例如列表或数
function TSomething.Concat(const E: IEnumerable>): IEnumerable; begin Result := TConcatIterator.Cr
我正试图找到解决这个问题的办法: 给定一个 IEnumerable> 我需要一个返回输入的方法/算法,但是如果多个 IEnumerable 具有相同的元素,则每个巧合/组只返回一个。 例如 I
我有一个有趣的问题:给定一个 IEnumerable , 是否有可能产生 IEnumerable> 的序列一次将相同的相邻字符串分组? 让我解释一下。 1。基本说明示例: 考虑以下 IEnumerab
我有课 public class Test { public void M1(IEnumerable> p) { } public void M2(IEnumerable)> p) {
我尝试解决下一个练习: 输入:整数列表 count >= 1;一些正整数 k 输出:此整数的所有可能元组,长度为 k ; 例如 输入: {1, 2}; k = 4 输出: { {1, 1, 1, 1
抱歉奇怪的标题。我想要实现的目标很简单: IEnumerable> listoflist; IEnumerable combined = listoflist.CombineStuff(); 例子:
公共(public)类项目 { ... public class Order { public List Items ... } public class Customer {
我有一个 IEnumerable>我想转换为单一维度集合的集合。是否可以使用通用扩展方法来实现这一点?现在我正在这样做以实现它。 List filteredCombinations = new Lis
我有一个 IEnumerable> CustomObject在哪里有一个 x (用作键(在本例中为 1 、 2 、 3 ))和 y值(value)。一些假数据: { { {1, 2}, {2, 4
我需要做的是选择嵌套元素列表,这是我的查询 returns IEnumerable>这是我的 linq 表达式: from a in (questions.Select(x => x.AnswerLi
如何使用 LINQ(或其他方式)将 IEnumerables 的 IEnumerable 拆分为一个平面 IEnumerable? 最佳答案 enumerable.SelectMany(x => x)
例如: public interface IEnumerable { IEnumerator GetEnumerator(); } //This interface allows the c
我对 Reflection.Emit 有疑问。我想要动态创建的类,它具有 ICollection 的简单实现。我定义的所有方法都很好,而不是接下来的两个: public IEnumerator Get
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Why was IEnumerable made covariant in C# 4? 我正在查看 MSDN
IEnumerator.MoveNext() 的实现是否预计会相对较快?或者如果“移动到下一项” 包括磁盘 IO、Web 请求或其他可能长时间运行的操作是否可以? 例如,我正在处理一个处理文档的项目,
以下代码创建了 List 的中间实例并在 yield 返回之前将值附加到它。有没有一种好的方法可以避免创建实例并直接 yield 返回单元格值? IEnumerable> GetStrValues()
我有两个 IEnumerable 对象,我想验证其中一个是否包含另一个的所有元素。 我正在使用 obj1.Intersect(obj2).Any() 但交集没有像我预期的那样工作。即使 obj2 中只
我正在尝试这个 MSDN page 上的例子.我试图更改 GetEnumerator 方法。我知道那似乎有些不对劲,但它符合要求然后就不会运行。错误是枚举器尚未启动,应该调用 MoveNext,但 它
我写过关于自定义 IEnumerator 的文章。从中生成 IEnumerable 的最简单方法是什么?理想的解决方案(一行代码)是是否有一些用于该目的的类。还是我必须自己创建? 最佳答案 不幸的是,
我是一名优秀的程序员,十分优秀!