LINQ to SQL的问题 - 没有支持转换为SQL(问题C#属性) [英] Linq To SQL problem - has no supported translation to SQL (problem with C# property)

查看:305
本文介绍了LINQ to SQL的问题 - 没有支持转换为SQL(问题C#属性)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我延长一些LINQ to SQL类。我有2类似的语句,一日一期工程,2号(没有支持转换为SQL的错误)。

  VAR REG2 = rs.ProductRegistrations()的SingleOrDefault(p => p.Product.product_name ==ACE)

VAR REG5 = rs.ProductRegistrations ().SingleOrDefault(p => p.product_name ==ACE);



看完这个链接的 http://stackoverflow.com/questions/953280/linq-no-translation-to-sql



我的理解(我认为),基本上都需要内联,否则表达式树不能正确计算。首届比如直接访问LinqToSql EntitySet的产品(藏在心里内置),而第二个示例使用这样定义的属性:

 公共部分类ProductRegistration:IProduct 
{
公共字符串PRODUCT_NAME
{
{返回this.Product.product_name; }
}
}



我假设我的问题是,LinqToSql不能翻译说。



我怎么会变成一个财产成等价的声明?我知道我需要使用System.Linq.Expressions.Expression,但一切我试过不工作(有的甚至不编译)。也许我应该让(使用表达式)扩展方法,然后调用从属性?属性可以调用扩展方法?



象下面这样事情不工作:

 公共静态系统.Linq.Expressions.Expression<&Func键LT; IProduct,布尔>> ProductName2(字符串PNAME)
{
回报率(P => p.product_name == PNAME);
}



底线,我知道我需要换我访问方式在表达式来; ....>,但我不知道如何访问从属性,使上述REG5变量将正常工作



会伟大的,如果有一些神奇的属性,我可以只添加到属性设置为自动式的财产,使LinqToSql快乐,而不是表达式来包装它,...>



很想能够做到这一点...

 公共部分类ProductRegistration:IProduct 
{
[自动表达式]
公共字符串PRODUCT_NAME
{
{返回this.Product.product_name; }
}
}

修改
以下作品的链接和答案。真棒,谢谢。我的问题的第二部分,我又得2类似的语句,一日一期工程,2号(没有支持转换为SQL的错误)。

$ B $ 。:b

  VAR章= rs.ProductRegistrations()产品名称(ACE)WithTranslations()的SingleOrDefault()。 

VAR REG2 = rs.ProductRegistrations2()产品名称(ACE)WithTranslations()的SingleOrDefault()。;



在它们的区别是,一日一返回具体类的一个IQueryable 的IQueryable [ProductRegistration],而第2个其中一个返回的的接口的的IQueryable [IProduct]一个IQueryable。我想用第二一个,因为我一巴掌可以在许多不同类的接口,它是更通用的方式,但它似乎不起作用。任何想法?


解决方案

将是巨大的,如果有一些神奇的属性,我可以只添加到该属性设置为自动式的财产,使LinqToSql快乐,而不是表达它包装< ...>




有是,非常接近。你仍然需要做的部分的工作,但达米安民警卫队和朋友已经完成了艰难的部分给你:的客户端性能和任何远程LINQ提供程序



很酷的事情是。它的工作原理与支持您使用表达式的LINQ提供程序



更新:与你的第二个版本的问题(与接口)是该可查询供应商将需要能够找出接口的实现是什么,因为它需要音译变成一只表名。但接口的整点是界面用户应该不可知作为对实施的类型,所以提供者将在交叉的目的与接口来工作。因此,我不认为第二种形式将工作。


I'm extending some Linq to SQL classes. I've got 2 similar statements, the 1st one works, the 2nd does not ("has no supported translation to SQL" error).

var reg2 = rs.ProductRegistrations().SingleOrDefault(p => p.Product.product_name == "ACE")

var reg5 = rs.ProductRegistrations().SingleOrDefault(p => p.product_name == "ACE");

After reading this link http://stackoverflow.com/questions/953280/linq-no-translation-to-sql

I understand (I think), that basically everything needs to be "inline", otherwise the expression tree can not be calculated correctly. The 1st example directly accesses the LinqToSql EntitySet "Product" (keeping everything inline), whereas the 2nd example uses a property that is defined like this:

public partial class ProductRegistration :IProduct
{
    public string product_name
    {
        get { return this.Product.product_name; }
    }
}

I'm assuming my problem is that LinqToSql cannot translate that.

How would I turn a "property" into an equivalent statement? I know I need to use the System.Linq.Expressions.Expression, but everything I've tried doesn't work (some don't even compile). Maybe I should make an Extension method (using Expression), and then call that from the property? Can a property call an extension method??

Things like below don't work:

public static System.Linq.Expressions.Expression<Func<IProduct, bool>> ProductName2 (string pname)
{
    return (p => p.product_name == pname);
}

Bottom line, I know I need to wrap my access method in an "Expression<....>" but I don't know how to access that from the property, so that the "reg5" variable above will work correctly.

Would be great if there was some magic attribute that I could just add to the property to "auto-expression" the property and make LinqToSql happy, instead of wrapping it in Expression<...>

Would love to be able to do this...

public partial class ProductRegistration :IProduct
{
    [Auto-Expression]
    public string product_name
    {
        get { return this.Product.product_name; }
    }
}

EDIT The link and answer below works. Awesome, thanks. 2nd part of my question, again I've got 2 similar statements, the 1st one works, the 2nd does not ("has no supported translation to SQL" error).

var reg = rs.ProductRegistrations().ProductName("ACE").WithTranslations().SingleOrDefault();

var reg2 = rs.ProductRegistrations2().ProductName("ACE").WithTranslations().SingleOrDefault();

The difference in them is that the 1st one returns an IQueryable of a concrete class "IQueryable[ProductRegistration]", whereas the 2nd one returns an IQueryable of an interface "IQueryable[IProduct]". I would like to use the 2nd one, because I can slap the interface across many different classes and it's more generic that way, but it seems to not work. Any ideas?

解决方案

Would be great if there was some magic attribute that I could just add to the property to "auto-expression" the property and make LinqToSql happy, instead of wrapping it in Expression<...>

There is, very nearly. You'll still have to do some work, but Damien Guard and friends have done the hard part for you: Client-side properties and any remote LINQ provider

The cool thing is that it works with any LINQ provider which supports the Expressions you use.

Update: The problem with your second version (with the interface) is that the Queryable provider would need to be able to figure out what the implementor of the interface is, because it needs to transliterate that into a table name. But the whole point of an interface is that the interface user should be agnostic as to the implementing type, so the provider would be working at cross purposes with the interface. So I don't think the second form will work.

这篇关于LINQ to SQL的问题 - 没有支持转换为SQL(问题C#属性)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆