Hi
I am trying below code but not working
C#
public Int32 subSum(Int32 Id)
{
try
{
var Result = (from t in context.Masters
where t.ID == Id
select new
{
SumCol1 = t.Sum(x => x.weight)
})SingleOrDefault;
return Result;
}
catch (Exception ex)
{
throw ex;
}
}Thanks

Vishal YelveRank #178 · 13.6K reputationPosted Oct 1, 2022, 8:13 PM EDT
Try this
var Result= (from t in Context.Mastsrs where t.ID == Id select t).Sum(x=> x.weight);Ramco RamcoRank #462 · 5.4K reputationPosted Oct 1, 2022, 11:36 PM EDT
Hi
It is giving error Cannot implicitly convert type int? to int. An explicit conversion exists
public Int32 subSum(Int32 Id)
{
try
{
var Result = (from t in context.Masters where t.ID == Id select t).Sum(x => x.SubWeight);
return Result;
}
catch (Exception ex)
{
throw ex;
}
}Thanks
Vishal JoshiRank #377 · 6.7K reputationPosted Oct 3, 2022, 2:10 AM EDT
Hello
Please try below linq query code.
var Result = (from t in masters where t.ID == Id && t.weight != null select t).Sum(x => (int)x.weight);