vendredi 4 septembre 2015

Converting VB to C# with Linq and Reflection

I'm working on a project converting some VB to C#. This particular Linq query works fine in VB, but it's giving me fits trying to convert.

The C# piece that I'm struggling with is the group by prop.GetProperty(stringColumn).GetValue(s, null).ToString() The compiler error is

Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.

It also notes that the s variable in the select does not exist.

VB

(From s In query Group By StringValue = 
    prop.GetProperty(stringColumn).GetValue(s, Nothing)
          Into NumTotal =
          Sum(Double.Parse(prop.GetProperty(numberColumn).GetValue(s, Nothing).ToString()))
          Order By NumTotal Descending Select StringValue, NumTotal)

C#

(from s in query group s by 
 new {prop.GetProperty(stringColumn).GetValue(s, null).ToString()} into NumTotal
    select new {
        StringValue = prop.GetProperty(stringColumn).GetValue(s, null),
        NumTotal = NumTotal.Sum(x =>
            Double.Parse(prop.GetProperty(numberColumn).GetValue(s, null).ToString()))})

I saw this, but I really didn't understand how the Expression stuff was supposed to work, and wasn't sure if it should be part of my solution. What am I missing in the conversion, and why would two seemingly similar queries behave so differently?

Thanks





Aucun commentaire:

Enregistrer un commentaire