简体   繁体   English

Linq To Entity字符串到int的转换顺序如下

[英]Linq To Entity string to int conversion in order by

There is a table in our database the stores year and month, each being a char. 我们的数据库中有一个表,存储着年份和月份,每个都是一个字符。 The existing Linq to Entity query looks like this: 现有的Linq to Entity查询如下所示:

from mc in repository.table.AsQueryable()
orderby mc.Year descending, mc.Month descending
select mc).FirstOrDefault()

The issue with this is that it is ordering by string and not int, leading to the incorrect row being returned from the query. 这样做的问题是它是按字符串而不是整数排序的,从而导致从查询返回错误的行。 I have tried to convert the year and month to int and datetime, both throwing an error saying: 我试图将年和月转换为int和datetime,都抛出一个错误:

"LINQ to Entities does not recognize the method 'System.DateTime ToDateTime(System.String)' method, and this method cannot be translated into a store expression." “ LINQ to Entities无法识别方法'System.DateTime ToDateTime(System.String)',并且该方法无法转换为商店表达式。”

(from mc in repository.table.AsQueryable()
 orderby mc.Year descending, Convert.ToDateTime(mc.Month) descending
 select mc).FirstOrDefault()

OR 要么

"LINQ to Entities does not recognize the method 'Int32 ToInt32(System.String)' method, and this method cannot be translated into a store expression." “ LINQ to Entities无法识别方法'Int32 ToInt32(System.String)',并且该方法无法转换为商店表达式。”

(from mc in repository.table.AsQueryable()
 orderby mc.Year descending, Convert.ToInt32(mc.Month) descending
 select mc).FirstOrDefault();

As you can see, I cannot cast the month and year into INTs first because it is using the data from the table, not external data. 如您所见,我不能首先将月份和年份转换为INT,因为它使用的是表中的数据,而不是外部数据。

PS: I do not have the authority to change the tables to make the two columns INTs. PS:我无权更改表以使两列为INT。

Anyone know how to approach this? 有人知道该如何处理吗?

我建议您看一下SqlFunctions类,最值得注意的是有关DatePart方法的信息,该方法可与Entity Framework一起使用(无例外),并将字符串转换为日期以供您正确排序。

As @IronMan84 noticed solution below works only if you have .edmx file. 正如@ IronMan84所注意到的,以下解决方案仅在具有.edmx文件的情况下有效。

  1. Add the following xml to your .edmx file. 将以下xml添加到您的.edmx文件。 (Just do a 'Find in Files' in Visual Studio </Schema> and place it before this node): (只需在Visual Studio </Schema>执行“查找文件”并将其放置在此节点之前):

    <Function Name="IntParse" ReturnType="Edm.Int32"> <Parameter Name="stringvalue" Type="Edm.String" /> <DefiningExpression> cast(stringvalue as Edm.Int32) </DefiningExpression> </Function>

  2. Add the following static function to your auto-generated Entity Framework class file: 将以下静态函数添加到自动生成的Entity Framework类文件中:

    public partial class YourObjectContext { [EdmFunction("YourModel", "IntParse")] public static double IntParse(string val) { return int.Parse(val); } }

  3. You are ready to go, just add the new function to your LINQ Query: 您可以开始使用,只需将新功能添加到LINQ查询中即可:

    from mc in repository.table.AsQueryable() orderby YourObjectContext.IntParse(mc.Year) descending, YourObjectContext.IntParse(mc.Month) descending select mc).FirstOrDefault()

您是否尝试过:

(from mc in repository.table.AsQueryable() orderby ((int)mc.Year) descending, ((int)mc.Month) descending select mc).FirstOrDefault();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM