Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 4 tables as following

table 1 Location A
C#
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; }
}


Table 2 location B
C#
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; }
}


Table 3 Location C
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; }

}

Table 4

C#
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; }

    }


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
C#
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);
            }
Posted
Updated 7-Jul-18 5:38am
v2

1 solution

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

C#
//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 )
 
Share this answer
 
Comments
Ahmed El-Badry 7-Jul-18 11:45am    
thank you very much :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900