C# Linq查询汇总金额列

C# Linq查询汇总金额列,c#,linq,entity-framework,lambda,C#,Linq,Entity Framework,Lambda,我想对amount列进行求和,该列如何进行求和,在求和之前应将其转换为int。首先,您使用的是SingleOrDefault,它只会选择一个值。您想改用Sum。此外,这里不需要匿名类型。您只需使用: var result = (from t1 in _dbEntities.Charges join t2 in _dbEntities.ChargesTypes on t1.ChargesTypeID equals t2.ID where t1

我想对
amount
列进行求和,该列如何进行求和,在求和之前应将其转换为
int
。首先,您使用的是
SingleOrDefault
,它只会选择一个值。您想改用
Sum
。此外,这里不需要匿名类型。您只需使用:

var result = (from t1 in _dbEntities.Charges
              join t2 in _dbEntities.ChargesTypes on t1.ChargesTypeID equals t2.ID
              where t1.StudentID == 1033
              select new {t2.Amount}).SingleOrDefault();
或者,您可以使用采用投影的
Sum
形式,可能使用不同形式的
Join
,因为您只需要
t2
值:

var result = (from t1 in _dbEntities.Charges
              join t2 in _dbEntities.ChargesTypes on t1.ChargesTypeID equals t2.ID
              where t1.StudentID == 1033
              select (int) t2.Ammount).Sum();
甚至在连接中转换,然后使用“普通”
Sum

var result = _dbEntities.Charges
                        .Join(_dbEntities.ChargesTypes,
                              t1 => t1.ChargesTypeID,
                              t2 => t2.ID,
                              (t1, t2) => t2)
                        .Sum(t2 => (int) t2.Ammount);
编辑:如果数据库中的
Amount
列(顺便说一句,它应该是
Amount
)是
NVARCHAR
,那么a)如果可能,请更改它。如果数据库类型合适,生活总是简单的;b) 如果无法执行以下操作,请使用
int.Parse

var result = _dbEntities.Charges
                        .Join(_dbEntities.ChargesTypes,
                              t1 => t1.ChargesTypeID,
                              t2 => t2.ID,
                              (t1, t2) => (int) t2.Ammount)
                        .Sum();
如果这不起作用,您可能需要在.NET端进行解析,例如

var result = (from t1 in _dbEntities.Charges
              join t2 in _dbEntities.ChargesTypes on t1.ChargesTypeID equals t2.ID
              where t1.StudentID == 1033
              select int.Parse(t2.Ammount)).Sum();

首先,您使用的是
SingleOrDefault
,它只选择一个值。您想改用
Sum
。此外,这里不需要匿名类型。您只需使用:

var result = (from t1 in _dbEntities.Charges
              join t2 in _dbEntities.ChargesTypes on t1.ChargesTypeID equals t2.ID
              where t1.StudentID == 1033
              select new {t2.Amount}).SingleOrDefault();
或者,您可以使用采用投影的
Sum
形式,可能使用不同形式的
Join
,因为您只需要
t2
值:

var result = (from t1 in _dbEntities.Charges
              join t2 in _dbEntities.ChargesTypes on t1.ChargesTypeID equals t2.ID
              where t1.StudentID == 1033
              select (int) t2.Ammount).Sum();
甚至在连接中转换,然后使用“普通”
Sum

var result = _dbEntities.Charges
                        .Join(_dbEntities.ChargesTypes,
                              t1 => t1.ChargesTypeID,
                              t2 => t2.ID,
                              (t1, t2) => t2)
                        .Sum(t2 => (int) t2.Ammount);
编辑:如果数据库中的
Amount
列(顺便说一句,它应该是
Amount
)是
NVARCHAR
,那么a)如果可能,请更改它。如果数据库类型合适,生活总是简单的;b) 如果无法执行以下操作,请使用
int.Parse

var result = _dbEntities.Charges
                        .Join(_dbEntities.ChargesTypes,
                              t1 => t1.ChargesTypeID,
                              t2 => t2.ID,
                              (t1, t2) => (int) t2.Ammount)
                        .Sum();
如果这不起作用,您可能需要在.NET端进行解析,例如

var result = (from t1 in _dbEntities.Charges
              join t2 in _dbEntities.ChargesTypes on t1.ChargesTypeID equals t2.ID
              where t1.StudentID == 1033
              select int.Parse(t2.Ammount)).Sum();

问题是db中的amount列是一种nvarchar类型,所以在这里如何将其转换为第一个intthat@Mike字体我怎么能猜到呢?您可以尝试使用
int.Parse
而不是强制转换,也可以获取所有字符串,然后求和。我将添加一个编辑-但请更加小心地将所有相关信息添加到问题中,以便将来开始。谢谢Jon。。我是实体的新手,你能指导我这个网站吗?我想从实体框架的基础上学习它。你的回复是appreciated@Mike当前位置我建议你买一本好书,或者看一个好的多学科课程。有很多可用的资源,但我自己没有太多的EF经验,所以没有太多的推荐方式。Julie Lerman有很好的Pluralsight课程,我相信。问题是db中的amount列是nvarchar的一种类型,所以在这里将其转换为第一个int怎么办that@Mike字体我怎么能猜到呢?您可以尝试使用
int.Parse
而不是强制转换,也可以获取所有字符串,然后求和。我将添加一个编辑-但请更加小心地将所有相关信息添加到问题中,以便将来开始。谢谢Jon。。我是实体的新手,你能指导我这个网站吗?我想从实体框架的基础上学习它。你的回复是appreciated@Mike当前位置我建议你买一本好书,或者看一个好的多学科课程。有很多可用的资源,但我自己没有太多的EF经验,所以没有太多的推荐方式。我相信朱莉·勒曼有很好的复视课程。