动态lambda表达式(OrderBy)和可空属性类型(Dynamic lambda expression (OrderBy) and nullable property type)
我正在尝试动态创建表达式,通过Entity Framework对数据库中的数据进行排序。 但我遇到了一个问题,无法克服它。 也许让我解释一下我想做什么。 我的目标是创建这样的表达式:
x => x.Property
其中“Property”是我想动态指定的属性名称。
现在,让我们转到类,它代表数据库中的表(我简化它以使事情更清晰):
public class MyModelClass { public long MyLongProperty { get; set; } public decimal? MyNullableDecimalProperty { get; set; } // IMPORTANT: it's nullable }
这是我的代码,我正在尝试创建前面描述的表达式:
// Db is EntityFramework context IQueryable<MyModelClass> list = Db.MyModels.Select(x => x); // x => var argument = Expression.Parameter(list.ElementType, "x"); // x.MyNullableDecimalProperty var propertyToOrder = Expression.Property(argument, "MyNullableDecimalProperty"); // x => x.MyNullableDecimalProperty var finalExpression = Expression.Call( typeof (Queryable), "OrderBy", new[] { list.ElementType, typeof(IComparable) }, list.Expression, Expression.Lambda<Func<MyModelClass, IComparable>>(propertyToOrder, argument)); list = list.Provider.CreateQuery<MyModelClass>(finalExpression);
第4个语句出现问题(var finalExpression = Expression.Call(...))。 我得到一个例外:
“System.Nullable`1 [System.Decimal]”类型的表达式不能用于返回类型“System.IComparable”。
据我所知,问题在于我使用“IComparable”类型,其中“MyNullableDecimalProperty”是Nullable,而Nullable不是用户IComparable接口。 当我通过“MyLongProperty”订购或更换“IComparable”时,不会抛出异常。
所以我的问题:
我应该使用什么类型来使它适用于任何可以为空的属性?
是否可以使用一种类型,它将适用于所有属性,无论它们是可空的还是不可空的。
注意:我知道我可以使用ex。 动态Linq库,但我对这个解决方案不感兴趣 - 我想学习如何在不使用第三方库的情况下克服它。
I'm trying to dynamically create expression that will sort data from database through Entity Framework. But I encountered one problem and cannot overcome it. Maybe let me explain what I'm trying to do. My goal is to create expression like this:
x => x.Property
Where "Property" is the name of property which I'd like to specify dynamically.
Now, let's go to the class, which represents the table in database (I simplified it to make things more clear):
public class MyModelClass { public long MyLongProperty { get; set; } public decimal? MyNullableDecimalProperty { get; set; } // IMPORTANT: it's nullable }
It is my code, where I'm trying to create expression described earlier:
// Db is EntityFramework context IQueryable<MyModelClass> list = Db.MyModels.Select(x => x); // x => var argument = Expression.Parameter(list.ElementType, "x"); // x.MyNullableDecimalProperty var propertyToOrder = Expression.Property(argument, "MyNullableDecimalProperty"); // x => x.MyNullableDecimalProperty var finalExpression = Expression.Call( typeof (Queryable), "OrderBy", new[] { list.ElementType, typeof(IComparable) }, list.Expression, Expression.Lambda<Func<MyModelClass, IComparable>>(propertyToOrder, argument)); list = list.Provider.CreateQuery<MyModelClass>(finalExpression);
Problem occurs at 4th statement (var finalExpression = Expression.Call(...)). I get an exception:
Expression of type „System.Nullable`1[System.Decimal]” cannot be used for return type „System.IComparable”.
As far I understand the problem is with me using "IComparable" type where "MyNullableDecimalProperty" is Nullable and Nullable doesn't user IComparable interface. The exception isn't thrown when I'm ordering by "MyLongProperty" or when I replace "IComparable".
So my questions:
What type should I use to make it work with any nullable properties?
Is it possible to use one type and it will work with all properties whether they are nullable or non-nullable.
Notice: I know I can use for ex. Dynamic Linq library, but I'm not interested in this solution - I'd like to learn how to overcome it without using 3rd party libraries.
原文:https://stackoverflow.com/questions/32098485
最满意答案
来自一个美丽的网站: http : //www.abeautifulsite.net/blog/2011/11/detecting-mobile-devices-with-javascript/
var isMobile = { Android: function() { return navigator.userAgent.match(/Android/i); }, BlackBerry: function() { return navigator.userAgent.match(/BlackBerry/i); }, iOS: function() { return navigator.userAgent.match(/iPhone|iPad|iPod/i); }, Opera: function() { return navigator.userAgent.match(/Opera Mini/i); }, Windows: function() { return navigator.userAgent.match(/IEMobile/i); }, any: function() { return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows()); } }; if(isMobile.any()){ // Mobile! } else { // It is desktop }
否则使用像device.js这样的库http://matthewhudson.me/projects/device.js/
From a beautiful site: http://www.abeautifulsite.net/blog/2011/11/detecting-mobile-devices-with-javascript/
var isMobile = { Android: function() { return navigator.userAgent.match(/Android/i); }, BlackBerry: function() { return navigator.userAgent.match(/BlackBerry/i); }, iOS: function() { return navigator.userAgent.match(/iPhone|iPad|iPod/i); }, Opera: function() { return navigator.userAgent.match(/Opera Mini/i); }, Windows: function() { return navigator.userAgent.match(/IEMobile/i); }, any: function() { return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows()); } }; if(isMobile.any()){ // Mobile! } else { // It is desktop }
Else use libraries like device.js http://matthewhudson.me/projects/device.js/
相关问答
更多-
TCP/IP模型是一个________。[2023-05-19]
a -
来自一个美丽的网站: http : //www.abeautifulsite.net/blog/2011/11/detecting-mobile-devices-with-javascript/ var isMobile = { Android: function() { return navigator.userAgent.match(/Android/i); }, BlackBerry: function() { ...
-
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
我使用这段代码: if (navigator.userAgent.match(/(iPhone|iPod|iPad|Android|BlackBerry|IEMobile)/)) { document.addEventListener("deviceready", onDeviceReady, false); } else { onDeviceReady(); //this is the browser } UPDATE 有很多其他方法可以检测手机是否在浏览器上运行,这是另一个很好的选择: var ...
-
对的,这是可能的。 您可以重建jQuery移动框架并仅使用所需的模块。 转到此处: http : //jquerymobile.com/download-builder/并选择您想要的内容。 *We’ve decoupled all of our plugins and have a clear dependency map which allows us to offer the download builder tool (Alpha) to let you build the leanest pack ...
-
我不仅需要flexbox前缀,还需要所有不同flexbox属性的前缀。 所以我也需要所有这些mixin: @mixin flex($values) { -webkit-box-flex: $values; -moz-box-flex: $values; -webkit-flex: $values; -ms-flex: $values; flex: $values; } @mixin flex-flow($values) { -webkit-flex-flow: $value ...
-
我在http://mobiforge.com中加入了WHOISSTAN的php函数示例: function is_mobile(){ $regex_match="/(nokia|iphone|android|motorola|^mot\-|softbank|foma|docomo|kddi|up\.browser|up\.link|"; $regex_match.="htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|we ...
-
在桌面浏览器和ipad浏览器上显示不同的网站(Website displaying differently on desktop browser vs ipad browser)[2022-06-10]
所以我得问一下是否正在使用CSS库? 类似的事情发生在我身上。 第一个问题是我的CSS库(bootstrap 3)自动响应。 这件事发生在我身上的另一个原因是因为iPad有视网膜显示器而我的桌面没有显示,但它并没有那么不同。 不知道你用什么工具来帮助建立我最好的猜测网站。 So this is extremely embarrassing but for whatever reason my code wasn't getting passed into the site. After some fiddl ... -
如何使用Material Design Lite仅定位桌面浏览器(How to use Material Design Lite to target only desktop browser)[2020-02-09]
https://getmdl.io/components/index.html#layout-section/grid 我在上面的链接中提供了MDL网格的参考。 如果没有启动像mdl-grid类和mdl-cell类这样的内容,那么内容就不应该响应,这样就可以让你像内容卡一样标记具有特定大小的内容,这些大小会根据设备调整大小。 如果您使用自己的样式表,则可以创建一个卡类,它具有您认为最适合桌面上项目的特定像素宽度。 https://getmdl.io/components/index.html#layout- ... -
在桌面浏览器中访问用户的GPS设备有哪些选择?(What are my options for accessing the user's GPS device in a desktop browser?)[2022-05-17]
我现在不知道任何支持此功能的浏览器,但W3C Geolocation API的设计方式使浏览器可以使用GPS来确定您的位置。 (现在我相信唯一真正使用GPS的浏览器是iPhone 3.0上的Mobile Safari。) 在Windows 7中, 位置平台可以使用任意数量的位置提供程序插件,包括用于GPS设备的插件。 我不相信IE8正式支持W3C Geolocation API,但有一些实验版本支持它,所以很快就会发生。 Looks like this GPS in Browser tool will le ...