Wednesday, December 9, 2009

Error : "The query contains references to items defined on a different data context " in LINQ queries

Solution

can't do a join across servers You could have two separate LINQ queries, one against each server, and then join those two together:

Examples

var q1 = // get your data from your first server
var q2 = // get your data from your second server
var ResultData = from r1 in q1
join r2 in q2 on r1.Key equals r2.Key
select new { r1.Value1, r2.Value2 };


Real Time Examples
First DataBase Query


var WNotMast = (from objClmType in objWebNotDC.WNotMasts
select new

{

objClmType.TimeStamp,

objClmType.WUserKey,

objClmType.Id

}).ToList();

Second Database Query


var WUsers = (from objUsers in objRegDC.WUsers

join objUsrGrp in objRegDC.WUserGroups on
objUsers.WUserGroupKey equals objUsrGrp.WUserGroupKey

select new

{

objUsers.Name,

objUsers.WUserKey,

objUsers.WUserGroupKey,

objUsrGrp.Dsc

}).ToList();

Joined Both Database result

var Result = (from objResult in WNotMast

join objUsr in WUsers on objResult.WUserKey equals
objUsr.WUserKey

where objResult.TimeStamp > dtStartDt &&



orderby objUsr.Dsc

select new

{

ID = objResult.Id,

Time = objResult.TimeStamp,

Dsc = objUsr.Dsc,

UserKey = objUsr.WUserKey,

Name = objUsr.Name


}).ToList();













1 comment:

  1. Thanks Muhilan it is working........:)

    ReplyDelete