2012-08-07 45 views
0

我有以下幾點:如何使用LINQ連接三個數據源?

var questions = questionService.Details(pk, rk); 
var topics = contentService.GetTitles("0006000") 
       .Where(x => x.RowKey.Substring(2, 2) != "00"); 
model = (
    from q in questions 
    join t in topics on q.RowKey.Substring(0, 4) equals t.RowKey into topics2 
    from t in topics2.DefaultIfEmpty() 
    select new Question.Grid { 
     PartitionKey = q.PartitionKey, 
     RowKey = q.RowKey, 
     Topic = t == null ? "No matching topic" : t.Title, 
     ... 

現在我需要添加以下內容:

var types = referenceService.Get("07"); 

哪裏有questions.typetypes.RowKey

之間的聯繫是沒有辦法,我可以鏈接的方式這第三個數據源,並且如果在類型表中沒有匹配的話,它會給出一條消息「No matching type」?我的問題是我只是不確定如何做下一個加入。

回答

0

我相信你可以加入的結果,先與第三數據源連接:

var questions = questionService.Details(pk, rk); 
var topics = contentService.GetTitles("0006000") 
       .Where(x => x.RowKey.Substring(2, 2) != "00"); 
var types = referenceService.Get("07"); 

model = (
    from q in questions 
    join t in topics on q.RowKey.Substring(0, 4) equals t.RowKey into topics2 
    from t in topics2.DefaultIfEmpty() 
    join type in types on q.type equals type.RowKey into types2 
    from type in types2.DefaultIfEmpty() 
    select new Question.Grid { 
     PartitionKey = q.PartitionKey, 
     RowKey = q.RowKey, 
     Topic = t == null ? "No matching topic" : t.Title, 
     Type = type == null ? "No matching type" : type.Title, 
     ... 
0
 var questions = questionService.Details(pk, rk); 
     var topics = contentService.GetTitles("0006000") 
         .Where(x => x.RowKey.Substring(2, 2) != "00"); 
     var types = referenceService.Get("07"); 
     model = (from q in questions 
      join t in topics on q.RowKey.Substring(0, 4) equals t.RowKey into topics2 
      from t in topics2.DefaultIfEmpty()) 
      select new Question.Grid { 
       PartitionKey = q.PartitionKey, 
       RowKey = q.RowKey, 
       Topic = t == null ? "No matching topic" : t.Title, 
       ... 
      }).Union 
      (from a in types 
      join q in questions on q.type equals a.RowKey 
      where a.ID != null 
      select new Question.Grid { 
        PartitionKey = q.PartitionKey, 
        RowKey = q.RowKey, 
        Topic = t == null ? "No matching topic" : t.Title, 
        ... 
      })