在服务请求或LINQ where子句中使用复杂类型 [英] Using complex type in service requests or LINQ where clauses

查看:55
本文介绍了在服务请求或LINQ where子句中使用复杂类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经公开了一个复杂类型的域模型,例如: Product具有Version属性,该属性是包含major,minor和build的自定义VersionType。这在从服务器获取数据到客户端时工作正常。

I have exposed a domain model with a complex type, e.g. Product has a Version property, which is a custom VersionType containing major, minor, build. This is working fine when getting data from the server to the client.

但是,当我尝试在服务器请求中使用复杂类型时,客户端会抛出一个错误,抱怨我的类型不能被转换为字符串。本质上,客户端使用linq / where查询,其参数包含复杂
类型的实例。

However, when I try to use the complex type in a server request then the client throws an error complaining that my type cannot be converted to a string. Essentially, the client is using a linq/where query with a parameter containing an instance of the complex type.

我明白,没有进一步提示,WCF无法知道如何将类型表示为请求URI的字符串。

I understand, that without further hints, WCF cannot know how to represent the type as a string for the request URI.

我需要做什么 - 在客户端和/或服务器端 - 使其工作?

What would I need to do - on the client and/or server side - to make this work?

推荐答案

URL查询语言不支持复杂类型文字,因此这是一个问题(以及你的原因)得到错误)。另一个问题是如何在其上定义相等运算符。

The URL query language doesn't support complex type literals so that's one problem (and the reason you get the error). The other problem would be how to define an equality operator on it.

解决方法是比较该复杂类型的所有属性。在您可以执行的URL中:

The workaround is to compare all the properties of that complex type. In the URL you can do:

/ Products?

/Products?


filter =(Version / Major eq 1)和(Version / Minor eq 2)
filter=(Version/Major eq 1) and (Version/Minor eq 2)

在LINQ中,这看起来像是:

In the LINQ this would look something like:

来自产品中的p

其中p.Version.Major == 1&& p.Version.Minor == 2

from p in Products
where p.Version.Major == 1 && p.Version.Minor == 2

这应该可以正常工作。

谢谢,


这篇关于在服务请求或LINQ where子句中使用复杂类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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