Int.Parse在LINQ表达式 [英] Int.Parse in Linq Expression

查看:234
本文介绍了Int.Parse在LINQ表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的LINQ表达式。我想计算为nvarchar场数值的总和。我用下面的代码来做到这一点。但我得到一个错误,当我尝试运行此

I have following linq expression. I want calculate sum of numeric values in Nvarchar field. I use following code to do this. But I get an error when I try to run this.

        var m = new MaterialModelContainer();

        var list = (from x in
                        (
                            from inv in m.INVs
                            join l in m.LIBs on inv.MESC equals l.MESC
                            join o in m.OUTs on inv.MESC equals o.MESC
                            join t in m.TRANs on inv.MESC equals t.MESC
                            where t.TYPE == "60"
                            select new
                                {
                                    l.MESC,
                                    l.LINE_NO,
                                    l.UNIT_LINE,
                                    Description = l.DES + " " + l.PART_NO,
                                    inv.NEW_QTY,
                                    o.PJ,
                                    o.DATE,
                                    o.QTY,
                                    o.QTY_REC,
                                    TranQty = t.QTY,
                                    tranDate = t.DATE

                                }
                        )
                    group x by
                        new
                            {
                                x.MESC,
                                x.LINE_NO,
                                x.UNIT_LINE,
                                x.Description,
                                x.NEW_QTY,
                                x.PJ,
                                x.DATE,
                                x.QTY,
                                x.QTY_REC
                            }
                    into g
                    select new
                        {
                            QTY_Consum_1 = g.Where(c => int.Parse(c.tranDate) >= cuDate && int.Parse(c.tranDate) <= endDate).Sum(d => int.Parse(d.TranQty))

                        }
                   ).ToList();



错误描述:搜索

LINQ实体无法识别方法的Int32
解析(System.String)'方法,和这种方法不能被翻译
分成一个存储表达式
结果

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

我该如何解决这个问题,写这样的代码比这更好的?

How can i solve this problem and write this code better than this?

我改变代码,这个

 select new
                            {
                                QTY_Consum_1 = g.Where(c => SqlFunctions.StringConvert(c.tranDate) >= cuDate && SqlFunctions.StringConvert(c.tranDate) <= endDate).Sum(d => SqlFunctions.StringConvert(d.TranQty)),
                               g.Key.MESC
                            }
                       ).ToList();



但得到这个错误

but get this error

推荐答案

您不能使用int类型。在
解析在何处,同比能改变代码这个

you can n't use int.parse in where , yoy can change code to this

        var list = (from x in
                        (
                            from inv in m.INVs
                            join l in m.LIBs on inv.MESC equals l.MESC
                            join o in m.OUTs on inv.MESC equals o.MESC
                            join t in m.TRANs on inv.MESC equals t.MESC
                            where t.TYPE == "60" && t.QTY!=""
                            select new
                                {
                                    l.MESC,
                                    l.LINE_NO,
                                    l.UNIT_LINE,
                                    Description = l.DES + " " + l.PART_NO,
                                    inv.NEW_QTY,
                                    o.PJ,
                                    o.DATE,
                                    o.QTY,
                                    o.QTY_REC,
                                    TranQty = t.QTY,
                                    tranDate = t.DATE

                                }
                        ).ToList()
                    group x by
                        new
                            {
                                x.MESC,
                                x.LINE_NO,
                                x.UNIT_LINE,
                                x.Description,
                                x.NEW_QTY,
                                x.PJ,
                                x.DATE,
                                x.QTY,
                                x.QTY_REC
                            }
                    into g
                    select new
                        {
                            QTY_Consum_1 = g.Where(c => int.Parse(c.tranDate) >= cuDate && int.Parse(c.tranDate) <= endDate).Sum(d => int.Parse(d.TranQty)),
                           g.Key.MESC
                        }
                   ).ToList();



拳头电话。即使用int.parse(varable)

fist call . ToList method after that use int.parse(varable)

有一个愉快的一天。

这篇关于Int.Parse在LINQ表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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