本文介绍了任何解决方法缺乏EF5.x为PadLeft支持?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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支持?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 21:35
查看更多