简体   繁体   English

将字符串转换为long类型并在asp.net MVC中的linq查询中使用

[英]Convert string to long type and use in a linq query within asp.net MVC

Is it possible within Linq in C#, to convert a string field in a database, to a long type - and use it in the query? 是否可以在C#中的Linq中将数据库中的字符串字段转换为long类型 - 并在查询中使用它?

Here, tme is a unix time (long) - but the field in the database, targetdate - is a string. 这里,tme是一个unix时间(长整数) - 但是数据库中的字段targetdate是一个字符串。

I've tried: 我试过了:

var qbt = db.Calls
.Where(x => x.team == id && long.Parse(x.targetdate) <= tme);

However I get the message: LINQ to Entities does not recognize the method 'Int64 Parse(System.String)' method, and this method cannot be translated into a store expression. 但是我收到消息: LINQ to Entities does not recognize the method 'Int64 Parse(System.String)' method, and this method cannot be translated into a store expression.

I know you can convert before the linq query, but is there any way of using it WITHIN the linq query? 我知道你可以在linq查询之前转换,但有没有办法在linq查询中使用它?

Thanks for any help, 谢谢你的帮助,

Mark 标记

This is to do with the way the Linq is translated into the backing query language, it might be easier to do a string comparison in this case, using tme.ToString() . 这与Linq转换为支持查询语言的方式有关,在这种情况下,使用tme.ToString()可能更容易进行字符串比较。 If you pull the full collection down first, you could query like this but that means what it says: pulling down the full unfiltered (or at least less filtered) set. 如果您先将完整集合拉下来,则可以像这样查询,但这意味着它的含义:拉下完整的未过滤(或至少过滤)集。

try 尝试

var qbt = db.Calls.ToList()
.Where(x => x.team == id && long.Parse(x.targetdate) <= tme);

if you have many records you can limit them by team first and then call ToList like below 如果您有许多记录,您可以先按团队限制它们,然后像​​下面一样调用ToList

var qbt = db.Calls.Where(x => x.team == id).ToList()
 .Where(i=>long.Parse(i.targetdate) <= tme);

Or You can use AsEnumerable 或者您可以使用AsEnumerable

var qbt = db.Calls.AsEnumerable()
.Where(x => x.team == id && long.Parse(x.targetdate) <= tme);

You have to either change the database table to not store a string (you could create a computed column that converts it to a long or create a view if you cannot modify the existing table) or compare the value as string. 您必须更改数据库表以不存储字符串(您可以创建一个计算列,将其转换为long或创建视图,如果您无法修改现有表)或将值作为字符串进行比较。 The reason is that Entity Framework LINQ provider does not understand long.Parse and there is no method in SqlFunctions class for this purpose. 原因是实体框架LINQ提供程序不理解long.Parse并且SqlFunctions类中没有用于此目的的方法。

var stringTme = tme.ToString(CultureInfo.InvariantCulture);

var qbt = db.Calls
    .Where(x => x.team == id && ((x.targetdate.Length < stringTme.Length)
      || (x.targetdate.Length == stringTme.Length && x.targetdate <= stringTme)));

You have to either change the database table to not store a string or compare the value as string. 您必须更改数据库表以不存储字符串或将值作为字符串进行比较。 The reason is that Entity Framework LINQ provider does not understand long.Parse and there is no method in SqlFunctions class for this purpose.please use long.Parse() 原因是实体框架LINQ提供程序不理解long.Parse并且SqlFunctions类中没有用于此目的的方法。请使用long.Parse()

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

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