c# - 实体框架中的不等于列表<>
问题描述
我有4个表如下:
表 1 位置 A
public class CustmLocationA
{
public int Location1_ID { get; set; }
public string Location1 { get; set; }
public string Location1_Descrip { get; set; }
public bool IsActive { get; set; }
}
表 2 位置 B
public class CustmLocationB
{
public int Location2_ID { get; set; }
public string Location2 { get; set; }
public string Location2_Descrip { get; set; }
public int Location1_ID { get; set; }
public bool IsActive { get; set; }
}
表 3 位置 C
public class CustmLocationC
{
public int Location3_ID { get; set; }
public string Location3 { get; set; }
public string Location3_Descrip { get; set; }
public int Location2_ID { get; set; }
public bool IsActive { get; set; }
//Location B data
public string Location2 { get; set; }
[NotMapped]
public bool LocBIsActive { get; set; }
//location A data
[NotMapped]
public int Location1_ID { get; set; }
[NotMapped]
public string Location1 { get; set; }
[NotMapped]
public bool LocAIsActive { get; set; }
[NotMapped]
public string LocAandB { get; set; }
}
表 4:
public class CustmContact
{
public int contactID { get; set; }
public int Location3_ID { get; set; }
public int UserID { get; set; }
public bool Notify { get; set; }
public bool Access { get; set; }
[NotMapped]
public string UserName { get; set; }
[NotMapped]
public string LocationAll { get; set; }
}
我试过的:
我试图在位置 A 和 B 表中选择所有位置 C 以及相关表信息,之后我需要在表联系人(第 4 个表)中选择选定的用户位置,之后我需要从选定位置中删除所有选定的用户位置,但我发现不能应用于这一行中 int 类型的操作数和 list of
(a.Location3_ID != (from z in db.contacts where z.UserID == UserID select z.Location3_ID))
//Get Un-selected location by user ID
public List GetSelectedLocByUID (int UserID = 0)
{
var data = (from LocC in db.locationsC
join LocB in db.locationsB on LocC.Location2_ID equals LocB.Location2_ID
join LocA in db.locationsA on LocB.Location1_ID equals LocA.Location1_ID
select new CustmLocationC
{
Location1_ID = LocA.Location1_ID,
Location1 = LocA.Location1,
LocAIsActive = LocA.IsActive,
Location2_ID = LocB.Location2_ID,
Location2 = LocB.Location2,
LocBIsActive = LocB.IsActive,
Location3_ID = LocC.Location3_ID,
Location3 = LocC.Location3,
Location3_Descrip = LocC.Location3_Descrip,
LocAandB = LocA.Location1 + "-" + LocB.Location2,
IsActive = LocC.IsActive
}).Where(a => a.LocAIsActive == true && a.LocBIsActive == true && a.IsActive == true
&& **a.Location3_ID != (from z in db.contacts where z.UserID == UserID select z.Location3_ID)**
).OrderBy(a => a.Location1).ToList();
return (data);
}
解决方案
(Bryian Tan 的回答)
当前的逻辑“a.Location3_ID != (from z in db.contacts where z.UserID == UserID select z.Location3_ID)”试图将一个位置与一个或多个位置进行比较。这不起作用,比如说 1 个苹果是否等于 1 个或更多苹果。
根据我在这里发布的内容
//store the list of Location3_ID for a user in a list
List<int> loc3s = (from z in db.contacts
where z.UserID == UserID
select z.Location3_ID).ToList();
//then replace a.Location3_ID != (from z in db.contacts where z.UserID == UserID select z.Location3_ID) with below
//return all the result where Location3_ID not belong to the user
!loc3s.Contains(a.Location3_ID )
推荐阅读
- reactjs - 在 react-native 中进行突变后如何更新阿波罗商店?
- node.js - 猫鼬的html表单输入名称
- python - 如果我搜索缺少的 BST 项目,为什么我的程序会崩溃?
- eloquent - 十月CMS范围不适用
- jpa - 如何在 JPA 中插入默认值?
- maven - gitflow-maven-plugin 权限被拒绝错误(jenkins-gitlab)
- rxjs - 大理石图生成器 java/javascript 用于使用 rxjava/rxjs 或反应器的文档
- c# - 如何从 BadRequestErrorMessageResult 获取错误消息?
- apache-kafka - 使用通配符删除 Kafka 消费者组
- mongodb - 我如何设计一个文档模型在 Mongodb 中包含多个集合