2011-06-19 44 views
1

我有下面的代碼工作,並得到我的單個實體的數據。Silverlight使用實體LINQ加入

LoadOperation<TimeForm> loadoperation = _cc.Load(_cc.GetTimeFormsQuery() 
      .Where(o => o.Start>= weekrange.startdate 
        && o.End<= weekrange.enddate 
        && o.USERID== "TEST")); 

不過,我有3個表,其連接到這個賽馬錶,在SQL我的查詢看起來是這樣的:

SELECT T* FROM TimeForm 
INNER JOIN CLIENT ON TimeForm.CODEID= CLIENT.CODEID 
INNER JOIN RATE ON TimeForm.RATEID= RATE.RATEID 
INNER JOIN TASK ON TimeForm.TASKID = TASK.TASKID 

這怎麼有可能與上述語法?我需要這些表中的一些值。

回答

0

嘗試是這樣的:

var query = context.TimeForm. 
      Join(context.CLIENT, 
      t => t.CODEID, c => c.CODEID , 
      (t, c) => new 
      { 
       PropertyA = t.ColumnA, 
       PropertyB = c.ColumnB      
      }).Join(context.RATE, 
        b => b.RATEID, r => r.RATEID, 
        (b, r) => new 
        { 
         PropertyC = b.ColumnC, 
         PropertyD = r.ColumnD        
        }).Join(context.TASK, 
          x => x.TASKID, t => t.TASKID, 
          (x,t) => new 
          { 
           PropertyE = x.ColumnE, 
           PropertyF = t.ColumnF 
          }); 

PropertyA,B等只是存在於類型的屬性,您可以使用它存儲數據從查詢返回。而ColumnA,B等等是連接中涉及的表中存在的列。您可以用查詢中的實際值替換這些值。

0

您需要轉到域服務文件(其中定義了GetTimeFormsQuery())。它會是這個樣子:

public IQueryable<TimeForm> GetTimeForms() { 
    return this.Context.TimeForm; 
} 

,並添加到它,所以它是這樣的:

public IQueryable<TimeForm> GetTimeForms() { 
    return this.Context.TimeForm 
     .Include("Client") // Assuming your property to see the client is called "Client" 
     .Include("Rate") // Same for "Rate" 
     .Include("Task"); // and "Task 
} 

或任何導航屬性被稱爲在TimeFrom實體。

Silverlight不會執行延遲加載,因此您必須在域服務的查詢中明確包含這些屬性。另外,在接受開始和結束日期以及userid的域服務上創建一個額外的方法可能是明智的做法,這樣您就不會每次都將整個表格拉過來。

public IQueryable<TimeForm> GetTimeFormsWithStartAndEnd(DateTime start, DateTime end, string userId) { 
    return this.Context.TimeForm 
     .Include("Client") // Assuming your property to see the client is called "Client" 
     .Include("Rate") // Same for "Rate" 
     .Include("Task") // and "Task 
     .Where(o => o.Start>= start 
       && o.End<= end 
       && o.USERID== userId)); 

} 

後重建你的web項目,你必須在這些3作爲參數你的Silverlight一個名爲GetTimeFormsWithStartAndEndQuery方法。

Goodluck!