2012-06-13 27 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转换为字符串尝试。一个或多个失败的人会告诉你你的类型问题在哪里。

我怀疑这个问题实际上与可空类型有关。