类型'System.Linq.IQueryable'1 [System.Int32]'不支持比较运算符 [英] Comparison operators not supported for type 'System.Linq.IQueryable`1[System.Int32]'
问题描述
我遇到了LTS问题 我的代码如下:
I met a issue with LTS My code as below:
return from p in _db.Companies
where p.Deleted == false &&
(from q in _db.Contacts_Companies
where q.ContactId == contactId && q.Deleted == false
select q.CompanyId).Equals(p.Id)
select p;
我知道问题来自CompanyId.Equals(Id) 它们都是Int32,但是我有点困惑,无法比较Int32? 如果我使用包含,它将不符合我的要求.
I know the issue comes from the CompanyId.Equals(Id) they are both Int32, but I am a bit confused that Int32 cannot be compared? If I use Contains, it wont match my requirement.
我该如何克服这个问题?
How could I overcome this issue?
推荐答案
问题源于您的子查询:
from q in _db.Contacts_Companies
where q.ContactId == contactId && q.Deleted == false
select q.CompanyId
这不会返回int
-它会返回(如错误消息中所示)IQueryable<int>
.
This doesn't return an int
- it returns (as shown in your error message) IQueryable<int>
.
也许您对linq的延迟执行语义有所了解?
Perhaps you're caught up on the deferred execution semantics of linq?
无论如何,要解决此问题,您需要将IQueryable<int>
转换为简单的int
.由于您总是希望得到一个结果,因此建议添加.First()
:
Anyway, to solve this you need to convert from the IQueryable<int>
to a simple int
. Since you're always expecting exactly one result, I'd suggest adding .First()
:
return from p in _db.Companies
where p.Deleted == false &&
(from q in _db.Contacts_Companies
where q.ContactId == contactId && q.Deleted == false
select q.CompanyId).First().Equals(p.Id)
select p;
这篇关于类型'System.Linq.IQueryable'1 [System.Int32]'不支持比较运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!