Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
More complicated with Linq
Message
From
11/04/2012 10:51:15
 
 
To
11/04/2012 10:12:34
General information
Forum:
ASP.NET
Category:
LINQ
Environment versions
Environment:
VB 9.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01540963
Message ID:
01541065
Views:
30
This message has been marked as a message which has helped to the initial question of the thread.
>>No. You can new up an existing type or an anonymous type - no other options. Well you could go down the DLR route (but I don't like it):
var anon = clients.Select(x => new { Surname = x.LastName, GivenName = x.FirstName }).FirstOrDefault();
>>DoSomething(anon);
Then:
   void DoSomething (dynamic d)
>>    {
>>        string Surname = d.Surname;
>>    }
(Sorry C# version but conversion should be simple)
>
>Maybe I am not understanding it, but what I see here is some code to, in fact, add the property in the returned object instead of the sub object itself. But, in order to accomplish this, this code seems to execute a method after the initial query and adjust to include an additional property. But, the initial query would still return all the fields, correct? If that is the case, as some of it is an object, isn't that object be returned as well in that query? Or, is it that it will only become available from a secondary call made to the object result, which will go again across the wire to get the sub object?

All I am trying to explain is that if you new up an instance of an existing class then that instance will have all of the fields of that class - no more (since you cannot add any) and no less (since you can't take any away). The alternatives are:

(a) Define a class which has a subset of the larger classes properties and new up that.
(b) Use an anonymous type - but you will get type safety only within the scope of where it was created.
(You can use the anonymous type outside of its scope if you use the dynamic type as above - but you lose type safety)

Of course you could do something really ugly like using a dynamic grab-bag for addtional stuff:
public class Client
{
   //.....
  //......    
    public dynamic AdditionalData { get; set; }
}
Leading to:
Client c = clients.Select(x => new Client
        {
            LastName = x.LastName,
            FirstName = x.FirstName,
            AdditionalData = new { Drivable = true }
        }).FirstOrDefault();

        UseClient(c);
So
   static void UseClient(Client c)
    {
        bool b = c.AdditionalData.Drivable;
    }
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform