c# - Linq Any 总是返回 true
问题描述
我已经使用 Linq to Entities 很多年了,但这是我第一次遇到这个问题。我有提示和项目表,每个提示可以有很多项目。我在数据库中只有 3 个项目。编辑项目时,我想确保GivenId字段对于具有相同提示的项目是唯一的。我用这个:
Item item =db.Items.FirstOrDefault(c => c.Id == id);
if (item != null)
{
if (db.Items
.Any(x => x.GivenId == newGivenId &&
x.Id != item.Id &&
x.TipId == item.TipId))
{
//newGivenId is either different or if it is the same,
//it belongs to the same item
}
else
{
//newGivenId already exists for an Item in the same tip
}
}
但
db.Items
.FirstOrDefault(x => x.GivenId == newGivenId &&
x.Id != item.Id &&
x.TipId == item.TipId);
null
按预期返回。
我知道我可以像这样使用它:
Item it = db.Items
.FirstOrDefault(x => x.GivenId == newGivenId &&
x.Id != item.Id &&
x.TipId == item.TipId);
if(it==null)
{
}
else
{
}
但我只想找出问题所在Any
。
PS:所有Id
s(其中3个)都是typeof(int)
编辑:
这是生成的 sql 查询:
DECLARE @p__linq__0 AS SQL_VARIANT;
DECLARE @p__linq__1 AS SQL_VARIANT;
DECLARE @p__linq__2 AS SQL_VARIANT;
SET @p__linq__0 = NULL;
SET @p__linq__1 = NULL;
SET @p__linq__2 = NULL;
SELECT
CASE WHEN ( EXISTS (SELECT
1 AS [C1]
FROM [dbo].[Items] AS [Extent1]
WHERE ([Extent1].[GivenId] = @p__linq__0) AND ([Extent1].[Id] <> @p__linq__1) AND (([Extent1].[TipId] = @p__linq__2) OR (([Extent1].[TipId] IS NULL) AND (@p__linq__2 IS NULL)))
)) THEN cast(1 as bit) ELSE cast(0 as bit) END AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable1]
EF生成的类:
public partial class Item
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Item()
{
this.Parts = new HashSet<Part>();
}
public int Id { get; set; }
public Nullable<int> TipId { get; set; }
public Nullable<int> GivenId { get; set; }
public string FileId { get; set; }
public Nullable<bool> Carve { get; set; }
public Nullable<bool> Mina { get; set; }
public Nullable<bool> Deleted { get; set; }
public virtual Tip Tip { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Part> Parts { get; set; }
}
public partial class Tip
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Tip()
{
this.Items = new HashSet<Item>();
}
public int Id { get; set; }
public string Name { get; set; }
public Nullable<int> ModelId { get; set; }
public Nullable<bool> Deleted { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Item> Items { get; set; }
public virtual Model Model { get; set; }
}
解决方案
推荐阅读
- python - Python 3.7 版中是否存在 Isinstance?
- docker - 如何升级 pod securityContext runAsUser: 1010 的 kubernetes 容器权限
- javascript - 完整的日历未显示在 Web 表单上;脚本未运行
- angular - Angular 9 升级在编译期间导致 @types 错误
- gcc - 未对齐的向量指针异常 (AVX512)
- node.js - npm 错误!在“...ttps://registry.npmjs”附近解析时 JSON 输入意外结束
- typescript - 通过 LSP 扩展中的 CodeLens 请求发送参数
- scala - 未从 Web 客户端发送或接收到 Web 套接字的消息
- r-markdown - Rmarkdown 中的引用:使用 .bib 文件中的标题大写
- c# - 资源管理器获取默认语言和只有一种文化特定的翻译,每种文化都返回英语