C# using linq
-
Friday, September 21, 2012 8:31 PM
I am working in a C# 2010 application where I have written the following lines of linq to sql code:
int TotCount = 0;
string[] PkgIDs = rptData.Transaction.Where(c =>c.Package_ID.StartsWith("rva") &&
c.Received_Date != null ).Select(c => c.Package_ID).ToArray();
foreach (string PkgID in PkgIDs)
{
var eCnt = rptData.Details.Where(c =>c.Package_ID == PkgID).Select(c =>c.TotalTrans);
TotCount = Convert.ToInt32(eCnt);
}
My problem line of code is: var eCnt = rptData.Details.Where(c =>c.Package_ID == PkgID).Select(c =>c.TotalTrans);I want to be able to obtain the value contained in the c.TotalTrans field and place the value in the eCnt field.
When I am stepping through the code and go to the line right after the line I listed above, I only see the sql equivalent value. I want to see the value for c.TotalTrans in the eCnt field.
I know what I need now is to actually make the linq to sql statement actually execute. That would be similar to the 'ToArray()'
statement listed above. However, I do not know what to change in the statement to actually obtain the value.Thus can you tell me what else I need to add to this statement so that I get my desired result?
All Replies
-
Friday, September 21, 2012 8:37 PMModerator
Since you're trying to get a single value, you can use First() [or Single()], ie:
var eCnt = rptData.Details.Where(c =>c.Package_ID == PkgID).Select(c => c.TotalTrans).First();
That will give you the value from the first item.
Reed Copsey, Jr. - http://reedcopsey.com
If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful". -
Friday, September 21, 2012 9:33 PM
When I do as you suggested, I get the following error message:
"Error 1 Cannot assign method group to an implicitly-typed local variable".
This logic is in a method of an object that is called.
I tried 'first' in a new C# app, and got the same method.
Thus can you tell me the following:
1. A different way to solve my problem, and/or
2. Another way to write this linq statement to obtain the same result?
-
Friday, September 21, 2012 10:51 PMModerator
Can you show what you used for code? You should only get that if you leave off the trailing () - with them, it's not a method group.
Reed Copsey, Jr. - http://reedcopsey.com
If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".- Marked As Answer by jazz_dog Saturday, September 22, 2012 8:39 PM
-
Saturday, September 22, 2012 6:15 AM
When I do as you suggested, I get the following error message:
"Error 1 Cannot assign method group to an implicitly-typed local variable".
Did you do as Reed Exactly showed you in the example with Opened and Closed parentheses on the end - after First (ie: First();).
One more thing, you should use FirsOrDefault method, in case if there is no element to return (if only using First() it will throw an exception).
Mitja
- Marked As Answer by jazz_dog Saturday, September 22, 2012 8:39 PM
-
Saturday, September 22, 2012 8:31 AM
var eCnt = rptData.Details.Where(c =>c.Package_ID == PkgID).Select(c => c.TotalTrans).First();
try removing First()
var eCnt = rptData.Details.Where(c =>c.Package_ID==PkgID)
.Select(c => c.totaltrans);
Or you can remove the select entirely
var mailgroup = emails.Values.Where(p =>IsValidFormat(p));rptData.Details.Where(c =>CallSomemethod(c.Package_ID))
or try
for var eCnt = rptData.Details.Where(c =>c.Package_ID == PkgID).Select(c =>c.TotalTrans);
TotCount = Convert.ToInt32(eCnt);try using var TotCount = Convert.ToInt32(eCnt);
Mark it as helpful if so!!! thanks, Mithilesh
- Marked As Answer by jazz_dog Saturday, September 22, 2012 8:39 PM
-
Saturday, September 22, 2012 8:39 AM
or I think the problem is with the 'string' type in foreach, try using var instead of string.
foreach (var PkgID in PkgIDs)
{
var eCnt = rptData.Details.Where(c =>c.Package_ID == PkgID).Select(c =>c.TotalTrans);
TotCount = Convert.ToInt32(eCnt);
}Mark it as helpful if so!!! thanks, Mithilesh
-
Monday, September 24, 2012 1:00 PM
or I think the problem is with the 'string' type in foreach, try using var instead of string.
Since PkgIDs is an array of strings, using var would change nothing. Since 'string' is only 3 characters longer than 'var', I recommend using 'string'.
- Edited by Louis.frMicrosoft Community Contributor Monday, September 24, 2012 1:02 PM

