在实体框架中不等于list <>

时间:2018-07-07 15:21:20

标签: c# linq

我有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之后,我需要在表Contact(第4个表)中选择选定的用户位置,之后我需要从选定的位置删除所有选定的用户位置位置,但我发现该行不能应用于int类型和此行中的列表

(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);
 } 

1 个答案:

答案 0 :(得分:0)

(Bryian Tan的回答)

当前逻辑“ a.Location3_ID!=(从db.contacts中的z,其中z.UserID == UserID选择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 )