在Silverlight中使用Linq进行域服务

时间:2014-12-11 02:36:07

标签: c# linq wcf silverlight

我有点内疚地问这个问题,因为之前问了一个熟悉的问题,但我没有得到明确的答案。我正在构建一个silverlight程序,我需要在域服务中创建一个Linq查询(使用wcf ria)。我需要计算这里的总和是我的代码

int lola = (from c in context.GetTRANSACTIONSQuery()
            where ((c.CHART_ACC == transStudID) && (c.sch_year == 13))
            select c).Sum();

MessageBox.Show(lola.ToString());

Sum()的括号之间,我收到一条错误

  

错误1实例参数:无法转换为' System.ServiceModel.DomainServices.Client.EntityQuery'到' System.Collections.Generic.IEnumerable'

我错过了什么?我知道这是语法问题,因为我是Linq的新手。请谢谢!

1 个答案:

答案 0 :(得分:0)

你在Sum()中加起来了什么?你能尝试一下:

int lola = (from c in context.GetTRANSACTIONSQuery()
            where ((c.CHART_ACC == transStudID) && (c.sch_year == 13))
            select c.PROPERTYTHATYOUWANTTOADDUP).Sum();

或者你想计算一下你的查询中有多少项?

int lola = (from c in context.GetTRANSACTIONSQuery()
            where ((c.CHART_ACC == transStudID) && (c.sch_year == 13))
            select c).Count();

编辑:

因此,您要添加AMOUNT属性:

decimal lola = (from c in context.GetTRANSACTIONSQuery()
            where ((c.CHART_ACC == transStudID) && (c.sch_year == 13))
            select (decimal)c.AMOUNT).Sum();

您还可以确保c.AMOUNT永远不会为空,这样您以后就不会遇到错误:

decimal lola = (from c in context.GetTRANSACTIONSQuery()
            where ((c.CHART_ACC == transStudID) && (c.sch_year == 13) && (c.AMOUNT != null))
            select (decimal)c.AMOUNT).Sum();
相关问题