任何解决方法缺乏EF5.x为PadLeft支持?

编程入门 行业动态 更新时间:2024-10-23 17:38:46
本文介绍了任何解决方法缺乏EF5.x为PadLeft支持?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我工作在MVC4和Entity Framework 5,最近一个应用程序在执行我查询时碰到这个例外来了。

  

{LINQ到实体无​​法识别方法'System.String PadLeft(的Int32,字符)的方法,而这种方法不能被翻译成店前pression。}

当我在过去遇到类似的错误跑,我刚才提出的查询外的变量,然后在LINQ语句中使用的变量。不幸的是,在这种情况下,我操纵行的结果,所以我不知道如何去说,或者是最好的方法。任何帮助将是AP preciated。我的查询是如下:

的IQueryable<&System.String GT; LEAPrograms = db.Datamart_Draft            。凡(其中=> where.snapshot_id == snapshot_id                &功放;&安培; !String.IsNullOrEmpty(where.entity_program))            。选择(SEL = GT;(sel.entity_program.PadLeft(PROGRAMLENGTH,'0')))。鲜明();

解决方案

我结束了创建一个列表,然后通过迭代的结果。当我在名单做了.Distinct(),这将其转换回一个IEnumerable。我不知道性能明智的,这是更好,但与一些努力,我认为一个PadLeft可以为LINQ创建于做实体大致相同的事情。

的IEnumerable<串GT; LEAPrograms = db.Datamart_Draft.Where(WH => wh.snapshot_id == snapshot_id&放大器;&安培;!String.IsNullOrEmpty(wh.entity_program))选择(SE = GT; se.entity_program)。.Distinct();        //创建并填充列表(因为LINQ到实体不支持PadLeft)        清单<串GT; PaddedPrograms =新的List<串GT;();        的foreach(在LEAPrograms VAR行)        {            PaddedPrograms.Add(row.PadLeft(4,'0'));        }        LEAPrograms = PaddedPrograms.Distinct();

I'm working on an app in MVC4 and Entity Framework 5 and recently came across this exception when executing my query.

{"LINQ to Entities does not recognize the method 'System.String PadLeft(Int32, Char)' method, and this method cannot be translated into a store expression."}

When I have run across similar errors in the past, I've just made a variable outside the query and then used the variable in the LINQ statement. Unfortunately, in this case I'm manipulating the row results so I'm not sure how to go about that or if that is the best method. Any help would be appreciated. My query is below:

IQueryable<System.String> LEAPrograms = db.Datamart_Draft .Where(where => where.snapshot_id == snapshot_id && !String.IsNullOrEmpty(where.entity_program)) .Select(sel => (sel.entity_program.PadLeft(PROGRAMLENGTH, '0'))).Distinct();

解决方案

I ended up creating a List then iterating through the results. When I do a .Distinct() on the List, it casts it back to an IEnumerable. I'm not sure performance-wise which is better, but with some effort I think a PadLeft could be created for LINQ to Entities that did approximately the same thing.

IEnumerable<String> LEAPrograms = db.Datamart_Draft.Where(wh => wh.snapshot_id == snapshot_id && !String.IsNullOrEmpty(wh.entity_program)).Select(se => se.entity_program).Distinct(); // create and populate a List (because LINQ to Entities doesn't support PadLeft) List<String> PaddedPrograms = new List<String>(); foreach (var row in LEAPrograms) { PaddedPrograms.Add(row.PadLeft(4, '0')); } LEAPrograms = PaddedPrograms.Distinct();

更多推荐

任何解决方法缺乏EF5.x为PadLeft支持?

本文发布于:2023-05-28 12:32:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/320979.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:词库加载错误:Could not find file &#039;D:\淘小白 高铁采集器win10\Configuration\Dict_Sto

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!