Ho un'espressione linq seguente. Voglio calcolare la somma dei valori numerici nel campo Nvarchar. Io uso il seguente codice per fare questo. Ma ottengo un errore quando provo a eseguire questo.Int.Parse in Linq Expression
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();
errore Descrizione:
LINQ to Entities non riconosce il metodo 'Int32 Parse (System.String)' metodo, e questo metodo non può essere tradotto in un'espressione negozio
come posso risolvere questo problema e scrivere questo c ode meglio di questo?
cambio il codice a questo
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();
ma ottengo questo errore
Forse questa risposta ti aiuterà: [Converti stringa in int in EF 4.0] (http://stackoverflow.com/questions/5754218/convert-string-to-int-in-ef-4-0) –