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

前端 未结 4 826
清酒与你
清酒与你 2021-01-21 13:43

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?

Here, tme is a unix time (long) - but the field in t

相关标签:
4条回答
  • 2021-01-21 14:04

    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(). 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.

    0 讨论(0)
  • 2021-01-21 14:12

    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

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

    Or You can use AsEnumerable

    var qbt = db.Calls.AsEnumerable()
    .Where(x => x.team == id && long.Parse(x.targetdate) <= tme);
    
    0 讨论(0)
  • 2021-01-21 14:14

    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. The reason is that Entity Framework LINQ provider does not understand long.Parse and there is no method in SqlFunctions class for this purpose.

    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)));
    
    0 讨论(0)
  • 2021-01-21 14:23

    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()

    0 讨论(0)
提交回复
热议问题