任何解决方法缺乏EF5.x为PadLeft支持? [英] Any workaround for lack of support for PadLeft in EF5.x?

查看:176
本文介绍了任何解决方法缺乏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支持?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆