不相等的列表<>在实体框架中 [英] Not equal list<> in entity framework
问题描述
我有4张桌子如下
表1位置A
I have 4 tables as following
table 1 Location 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
Table 2 location B
public class CustmLocationB{
pubblic 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
Table 3 Location 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
Table 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; }
}
我的尝试:
我正在尝试选择所有位置C以及位置A和B中的相关表格信息之后我需要在表格中选择所选用户位置联系人(第4张表格)之后我需要从所选位置删除所有选定的用户表但我发现无法应用于int类型的操作数和< int>列表在这一行(a.Location3_ID!=(来自db.contacts中的z,其中z.UserID == UserID选择z.Location3_ID))
//按用户ID获取未选择的位置
What I have tried:
I'm trying to select all location C with related tables information in location A and B after that I need to select the selected user locations in table Contact (4th table) after that I need to remove all selected user table from selected locations but I found that cannot be applied to operands of type int and list of <int> in this line (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);
}
推荐答案
当前逻辑a.Location3_ID!=(来自db.contacts中的z,其中z.UserID = = UserID select z.Location3_ID)正在尝试将一个位置与一个或多个位置进行比较。哪个不行,就像说一个苹果等于1个或更多苹果。
根据发布的内容,我建议
The current logic "a.Location3_ID != (from z in db.contacts where z.UserID == UserID select z.Location3_ID)" is trying to compare ONE location with ONE or MANY location. Which will not work, like saying if 1 apple equals to 1 or more apple.
Based on what posted here, I would suggest
//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 )
这篇关于不相等的列表<>在实体框架中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!