2014-10-09 118 views
0

我需要幫助轉換的一些SQL選擇到LINQ轉換SQL到LINQ

原來這裏是SQL:

Select Top 1 IsNull(ct.Template, t.Template) as Template 
From Template t 
Left Outer Join ClientTemplate ct On t.TemplateTypeId = ct.TemplateTypeId And ct.ClientId = 149 
Where t.TemplateTypeId = (Select TemplateTypeId From QuoteType Where QuoteTypeId = 7) 
Order By t.Version DESC, ct.Version DESC 

我使用實體框架,並有QuoteTypes,ClientTemplates和模板實體。

上述SQL從客戶端149的ClientTemplate表中獲取模板(在實際代碼中使用變量)用於特定的QuoteType。如果CLientTemplate表中沒有條目,那麼它會從相同QuoteType的主Template表中返回Template!

我的想法是首先查詢QuoteType,然後查詢ClientTemplate表,看看是否存在,如果不是查詢模板表。問題是這會導致三個查詢,但我相信它可以一舉完成!?

任何人都可以爲我編寫LINQ嗎?

這裏是我的爛攤子至今:

QuoteType quoteType = (from qt in this.entities.QuoteTypes where qt.QuoteTypeID == this.SelectedNewQuoteTypeID select qt).First(); 
if (quoteType != null && quoteType.TemplateTypeID.HasValue) 
{ 
    int quoteTypeTemplateTypeID = (int)quoteType.TemplateTypeID; 

    var query = (from t in this.entities.Templates 
      join ct in this.entities.ClientTemplates on t.TemplateTypeID equals ct.TemplateTypeID 
      into a 
      from b in a.DefaultIfEmpty(new ClientTemplate()) 
      where t.TemplateTypeID == quoteTypeTemplateTypeID 
      orderby t.Version descending 
      select new 
      { 
       T1 = t.Template1, 
       T2 = b.Template 
      }).First(); 

    // Check the query to see if T1 and T2 are null and use whichever one isn't! 
    // TODO !!! 
} 
else 
{ 
    return string.Empty; 
} 

我有點放棄了,一旦我得到的遠遠張貼了這個!我的例子仍然有兩個查詢,並且不根據客戶端ID進行選擇。它在客戶端模板表上也沒有第二順序。

我已經繼承了原始的SQL語句,所以也許問題在於被寫得很糟糕!

過你......

+0

您也可以在您的數據庫中創建一個視圖或存儲過程來封裝此SQL代碼,然後通過LINQ調用它。 – RBarryYoung 2014-10-09 19:11:42

+0

僅供參考,SQL查詢對我來說看起來很好。儘管如果您將WHERE子句中的子查詢更改爲JOIN,但對LINQ來說可能更容易(不確定)。 – RBarryYoung 2014-10-09 19:13:35

+0

使用導航屬性代替連接。如果您需要幫助,請顯示您的實體課程,包括這些屬性。 – 2014-10-09 20:04:42

回答

1

我沒有測試它,但也許你可以嘗試這樣的事情

from t in this.entities.Template 
from ct in this.entities.ClientTemplate.Where(x => t.TemplateTypeId == ct.TemplateTypeId && ct.ClientId == 149).DefaultIfEmpty() 
where t.TemplateTypeId == (from x in this.entities.QuoteType where x.QuoteTypeId == 7 select x.TemplateTypeId).FirstOrDefault() 
orderby t.Version descending, ct.Version descending 
select new { ct.Template == null ? t.Template : ct.Template } 
0

我想這可能工作,雖然這是未經測試,從閱讀它出現你不能將多個連接條件添加到LinQ語句中,所以你可以在where子句中指定它

因此,在這裏,我試圖將SQL單詞轉換爲單詞,所以這是我的嘗試。如果它做到了一切,我認爲它會做到這一點。

int templateTypeId; 
templateTypeId= (context.QuoteType.Where(x => x.QuoteTypeId == 7)).FirstOrDefault().TemplateTypeId 

var qry =(

from t in context.Template 
join ct in context.ClientTemplate on 
t.TemplateTypeId equals ct.TemplateTypeId into cts 

from ct in cts.DefaultIfEmpty() //left join 

where t.TemplateTypeId == templateTypeId 
     && ct.ClientId == 149 
order by t.Version descending, ct.Version descending 
select new 
{ 
    Template = (ct.Template != null) ? ct.Template : t.Template //ternary operator 
}).FirstOrDefault(); 
+0

https://code.msdn.microsoft.com/LINQ-Join-Operators-dabef4e9 – CSharper 2014-10-09 19:34:00