I have the following function in VB.net and am trying to convert it to C#, but I have having difficulty. Could someone please help me out?
Here is the code:
Dim subset = Sheets.Where(Function(kvp As KeyValuePair(Of Integer, Sheet)) kvp.Value.Members.Where(Function(m As Member) m.MemberType = MemberType).Count() > 0)
subset.ToList().ForEach(Sub(kvp As KeyValuePair(Of Integer, Sheet)) WeightByType += (kvp.Value.Members.Sum(Function(m As Member) Convert.ToDecimal(m.TotalWeight, CultureInfo.InvariantCulture))))
It would be something like:
var subset = Sheets.Where(kvp => kvp.Value.Members.Where(m => m.MemberType == MemberType).Count()> 0);
subset.ToList().ForEach(kvp => WeightByType += (kvp.Value.Members.Sum(m => Convert.ToDecimal(m.TotalWeight, CultureInfo.InvariantCulture))));
That can all be simplified to the following
return Sheets
.Where(kvp => kvp.Value.Members.Where(m => m.MemberType == MemberType).Count() > 0)
.Sum(kvp => kvp.Value.Members.Sum(m => Convert.ToDecimal(m.TotalWeight, CultureInfo.InvariantCulture)));
I took both of the answers and used them. The new code looks like this:
decimal wbt = 0;
wbt += MbrSheets
.Where(kvp => kvp.Value.Members.Where(m => m.MemberType == MemberType).Count() > 0)
.Sum(kvp => kvp.Value.Members.Sum(m => Convert.ToDecimal(m.TotalWeight, CultureInfo.InvariantCulture)));
return wbt;
Not sure if you know about this, but there is a pretty good online code converter that Telerik has had up and running for a long time now.
I've had pretty good luck with it in the past. It will convert from VB to C# and vice versa.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.