是否有通用的方法来检查Linq提供程序(尤其是OData)是否支持属性定义? [英] Is there a general method to check whether a property define supported by a Linq provider, especially OData?

查看:56
本文介绍了是否有通用的方法来检查Linq提供程序(尤其是OData)是否支持属性定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在LinqPad中使用NorthWind.sdf成功运行了以下语句:

from s in Shippers
    select new
{
    s.ShipperID,
    s.CompanyName,      
    Count=s.ShipViaOrders.Count()       
}

同时,我无法在Odata服务中运行类似的语句( http: //LinqPad中的//services.odata.org/northwind/northwind.svc ):

from s in Shippers    
select new
{
    s.ShipperID,
    s.CompanyName,      
    Count=s.Orders.Count()      
}

错误是不支持使用表达式s.Orders.Count()构建或初始化类型为<> f__AnonymousType0`3 [System.Int32,System.String,System.Int32]的实例.". /p>

我知道Linq支持中OData服务非常有限.我的应用程序中有动态的Linq语句支持.实际上,我正在尝试将数据源从Compact SQL Server迁移到OData服务.

因此,我必须以一般方式处理NotSupportedException.目前,我尝试在运行属性定义之前检查其属性,例如

"s.Orders.Count() as Count"   

它通过了我的检查,但遇到了OData的NotSupportedException.

Linq提供程序是否可以检查属性定义(通过字符串还是lambda)?

任何建议都值得赞赏.

Y

解决方案

不幸的是,没有通用的编程方式来检查LINQ提供程序是否能够翻译任何给定的查询.通常,您将不得不求助于文档或(一定要)在尝试时实际尝试查询.

但是,不同的提供程序可能会提供某种机制来生成查询的某种表示形式,您可以使用该机制来检查查询是否可以运行而不必执行查询.

在OData客户端的情况下,您可以在查询上调用.ToString(),如果它能够成功处理查询,则它应该返回一个URL;否则,它应该返回一个URL.否则,它将返回类似于将Linq表达式转换为URI的错误:..."(实际的错误消息可能会因用户语言而异,但是肯定不是有效的URI).

I successfully ran the following statement with the NorthWind.sdf in LinqPad:

from s in Shippers
    select new
{
    s.ShipperID,
    s.CompanyName,      
    Count=s.ShipViaOrders.Count()       
}

At the same time , I failed to run a similar statement with the Odata Service (http://services.odata.org/northwind/northwind.svc) in LinqPad:

from s in Shippers    
select new
{
    s.ShipperID,
    s.CompanyName,      
    Count=s.Orders.Count()      
}

The error is "Constructing or initializing instances of the type <>f__AnonymousType0`3[System.Int32,System.String,System.Int32] with the expression s.Orders.Count() is not supported.".

I know OData service is very limited in Linq Support. I have dynamic Linq statement support in my application. Actually I am trying to migrate the datasource from Compact SQL Server to OData service.

So I have to deal with NotSupportedException in a general way. At present, I try to check the syntax of property define before running it, such as

"s.Orders.Count() as Count"   

It passed my check, but it met NotSupportedException of OData.

Is there a way to check whether a property define (by a string or lambda) is supported by a Linq provider?

Any suggestions are appreciated.

Ying

解决方案

Unfortunately there is no general programmatic way of checking whether a LINQ provider will be able to translate any given query. Typically you'll have to resort to documentation or (to be sure) actually try out the queries as you are doing.

Different providers may, however, provide some mechanism of generating some representation for the query, which you may be able to use to check whether the query would work without having to execute it.

In the OData client case, you can invoke .ToString() on the query, and it should return a URL if it's able to successfully process the query; otherwise it will return an error message that looks similar to 'Error translating Linq expression to URI: ...' (the actual error message may change depending on the user language, but it will definitely not be a valid URI).

这篇关于是否有通用的方法来检查Linq提供程序(尤其是OData)是否支持属性定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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