Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi ,

I have some parent child relation. Country is parent and based on that I want to retrive only his child data. I know country id which is coming from other table.

Curretly I am retriving countryid from user table then applyng some join where I am checking the start point which is my county id to his end point. which mean where other country will end. So my end point will be where records starts at null as a parent id.

I want to use only joins which will brings the data in one way.

Please see my below query. is there any way where I can join user table insted of bringing base country id first and then joining data which is also not my right way i have done.

please suggest

What I have tried:

int baseCountryId = context.Users.Where(u => u.UserID == userID).Single().BaseCountryID;

var loc = from l in context.Locations.Where(lo => !lo.IsInActive && lo.LocationID > baseCountryId
&& (lo.ParentLocationID == null || lo.LocationTypeID == territoryTypeID))
join p in context.Locations on l.ParentLocationID equals p.LocationID
into leftJoined
from lj in leftJoined.DefaultIfEmpty()
join ul in context.UserToLocations.Where(u => u.UserID == userID && !u.IsInActive) on l.LocationID equals ul.LocationID
into joined
from j in joined.DefaultIfEmpty()
where !l.IsInActive && j.UserID == null
select new
{
TerritoryID = l.LocationID,
TerritoryCode = l.Code,
TerritoryName = l.Name,
TerritoryParent = lj.Name,
ParentLocationID = l.ParentLocationID
};

foreach (var territory in loc)
{
if (territory.ParentLocationID == null)
{
break;
}
territories.Add(new LocationBO
{
LocationID = territory.TerritoryID,
Code = territory.TerritoryCode,
Name = territory.TerritoryName,
ParentLocation = territory.TerritoryParent
});
}
Posted
Updated 29-Feb-16 22:05pm
v2
Comments
Torakami 1-Mar-16 4:02am    
2 APIN APIN NULL NULL 1
3 APIN-GJ India NULL 2 2
4 APIN-IN India NULL 3 3
5 APIN-IN-GJ Gujarat NULL 4 4
6 APIN-IN-GJ-ADI Ahmedabad NULL 5 5
181 APIN-IN-MH Maharashtra NULL 4 4
189 LC01 test NULL NULL 1
192 LC-CO-01 INDIA NULL 3 3
193 LC-CO-02 JAPAN NULL 3 3
200 APIN-IN-GJ-BRC Baroda NULL 5 5
217 APIN-IN-MP Madhya Pradesh NULL 4 4
219 L1 Loc1 NULL 2 2
220 L2 Tổ chức NULL NULL 1
John C Rayan 1-Mar-16 4:52am    
What's the issue you face when you join context.Users and context.Locations based on the condition l.physicalLocationID > usr.BaseCountryID
Torakami 1-Mar-16 4:58am    
I am not facing any issue . I am getting proper records.
But is there any way where I can include user table into join to achive the same by somehow.

My join looks very complex due to the way I am doing
F-ES Sitecore 1-Mar-16 7:57am    
Google "Entity framework Include" and "Entity framework disable lazy loading". You basically need to use Include to join the tables and doing it without lazy loading means a single SQL statement will be used.
John C Rayan 1-Mar-16 8:37am    
What I meant was why can't you use something like this in your query at the beginning.

from usr context.Users.Where(u => u.UserID == userID)
join l in context.Locations.Where(lo => !lo.IsInActive && (lo.ParentLocationID == null || lo.LocationTypeID == territoryTypeID))
on lo.LocationID > usr.BaseCountryId into ...

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