2012-06-13 28 views
-3

加入我有一個LINQ查詢作爲LINQ到SQL查詢錯誤使用時有多個條件

from elements in context.Catalog_Element 
    join elementattributevalues in context.Catalog_ElementAttributeValue 
     on elements.ElementID equals elementattributevalues.ElementID 
    join allowedsubcomponents in context.Catalog_AllowedSubComponents 
     on new { AttributeValueID = elementattributevalues.AttributeValueID, 
        ElementClassID = elements.ElementClassID } 
     equals new { AttributeValueID = allowedsubcomponents.AttributeValueIDFilter, 
        ElementClassID = allowedsubcomponents.ClassIDFilter } 
    where allowedsubcomponents.ElementID == 
      new Guid("8c139311-f7cd-4961-a8bb-0d8dd923049e") 
    select new 
    { 
     elements.ElementNumber, 
     elements.Description 
    }) 

這是我展示了語法錯誤作爲一個表達式的加入子句中的類型不正確。請幫忙。

+0

這似乎不是有效的編譯的C#代碼。請糾正它。也請解釋你正在嘗試做什麼? Catalog_Element代表什麼?什麼是ElementAttributeValues? – Arran

+0

更正了代碼,我試圖獲取數據三個表catalog_element,catalog_elementAttributevalue,&catalog_allowedsubcomponents。 catalog_element是一個表,其中Elementattribute值是catalog_elementAttributevalue的別名。我已經檢查過匿名類型和數據類型,並且第二次連接的語法仍然顯示錯誤 –

回答

1

錯誤信息清楚地告訴你,你的類型不匹配,所以你確實需要確認類型。

我會爲四個領域(分別更改表和字段)這樣做是爲了看看他們類型報告:

// look at these in debugger or write them to console 
var myFirstType = context.Catalog_ElementAttributeValue 
    .Take(1) 
    .Select (x => x.AttributeValueID) 
    .GetType() 
    .ToString(); 

var mySecondType = ..... 

此外,您還可以快速看一看你的加入條款迫使你的數據串只是爲了看看它的工作原理:

on new 
{ 
    AttributeValueID = elementattributevalues.AttributeValueID.ToString(), 
    ElementClassID = elements.ElementClassID.ToString() 
} 
equals new 
{ 
    AttributeValueID = allowedsubcomponents.AttributeValueIDFilter.ToString(), 
    ElementClassID = allowedsubcomponents.ClassIDFilter.ToString() 
} 

然後只AttributeValueID轉換爲字符串嘗試,最後只有ElementClassID轉換爲字符串嘗試。一個或多個失敗的人會告訴你你的類型問題在哪裏。

我懷疑這個問題實際上與可空類型有關。