tags:

views:

53

answers:

2

I'm trying to generate a chart of the form:

User A    User B     Owes      Owed     Net
Sam       David      $20       $10      $10
Timbo     ODP        $30       $0       $30

Using the following query:

        var debt = from user in users
                   select new {
                     Username = username, 
                     User = user,
                     Owes = owedBetween(username, user),
                     Owed = owedBetween(user, username),
                     Net = Owes - Owed // doesn't compile
                   };

The problem is that that last line doesn't compile. Is there a way to set up the Net value in the query, or must I initialize it to zero then change it afterwards?

+1  A: 

Try:

Net = owedBetween(username, user) - owedBetween(user, username)

By the way, I suggest you use better names for username and user. Which is which?

CesarGon
yeah, I know, I'm going to refactor soon.
Rosarch
Great :-) ......
CesarGon
+14  A: 

Try using the let keyword:

  var debt = from user in users
               let owes = owedBetween(username, user)
               let owed = owedBetween(user, username)
               select new {
                 Username = username, 
                 User = user,
                 Owes = owes,
                 Owed = owed,
                 Net = owes - owed
               };
Dave Markle
+1 Elegant.....
CesarGon
+1: `let` is exactly what is necessary.
Ani