HighTechTalks DotNet Forums  

new pattern needed to declare container for var

Dotnet Framework (CLR) microsoft.public.dotnet.framework.clr


Discuss new pattern needed to declare container for var in the Dotnet Framework (CLR) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
ChrisA
 
Posts: n/a

Default new pattern needed to declare container for var - 12-30-2007 , 09:04 PM






Hi,
I am trying to declare a container and then add anonymous types to it. I
want to end up with something I can use with LINQ, but the normal way to load
a list doesn't work -- I don't know how to declare the target container. the
syntax should be something like: var[] list = <??>; but I need to declare
the container before I have a var.

TYPE list = new TYPE(); // I don't know how to declare a list of anonymous?
foreach (string nm in someList)
{
list.Add( new { Name = nm, Company = aComp };
}

I need a new pattern for LINQ.
Thanks

Reply With Quote
  #2  
Old   
Jon Skeet [C# MVP]
 
Posts: n/a

Default Re: new pattern needed to declare container for var - 12-31-2007 , 02:24 AM






(This would be better off in the C# group, btw - the CLR is unaware of
anonymous types.)

ChrisA <ChrisA (AT) discussions (DOT) microsoft.com> wrote:
Quote:
I am trying to declare a container and then add anonymous types to it. I
want to end up with something I can use with LINQ, but the normal way to load
a list doesn't work -- I don't know how to declare the target container. the
syntax should be something like: var[] list = <??>; but I need to declare
the container before I have a var.

TYPE list = new TYPE(); // I don't know how to declare a list of anonymous?
foreach (string nm in someList)
{
list.Add( new { Name = nm, Company = aComp };
}

I need a new pattern for LINQ.
You can't declare the target container ahead of time in that way. Two
options:

1) Use LINQ queries instead of manually adding things to the list. For
instance:

var list = someList.Select(x => new { Name = x, Company = aComp })
.ToList();

2) Create the list using type inference and a generic method which just
creates an empty list, e.g.

var list = CreateEmptyList(new { Name="Dummy", Company="Dummy" };
....
List<T> CreateEmptyList<T> (T sampleElement)
{
return new List<T>();
}

Personally I'd prefer the first route.

--
Jon Skeet - <skeet (AT) pobox (DOT) com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk


Reply With Quote
  #3  
Old   
ChrisA
 
Posts: n/a

Default Re: new pattern needed to declare container for var - 12-31-2007 , 01:16 PM



Jon,
Sorry about posting to the wrong area. I didn't realize anonymous types
weren't in the CLR.

I think I can get your first suggestion to work. Is it possible to
declare a variable inside the lambda? I need something like:
(x =>
string[] fields = x.Split();
new { name = fields[0], company = fields[1];
)
but I'm getting compiler errors.

I appologize for taking up your time -- I clearly have a lot of reading to
do before I've got this down.

Thanks

Reply With Quote
  #4  
Old   
ChrisA
 
Posts: n/a

Default Re: new pattern needed to declare container for var - 12-31-2007 , 01:33 PM



Jon,
Got it - just needed the brackets.

Thanks for the help. The new 3.5 features are even better than I
expected, now that I'm starting to build real code.

Reply With Quote
  #5  
Old   
Jon Skeet [C# MVP]
 
Posts: n/a

Default Re: new pattern needed to declare container for var - 01-01-2008 , 10:06 AM



ChrisA <ChrisA (AT) discussions (DOT) microsoft.com> wrote:
Quote:
Sorry about posting to the wrong area. I didn't realize anonymous types
weren't in the CLR.
No problem It's often not made terribly clear which things are
language features, which are framework (library features) and which are
CLR features. The CLR for .NET 3.5 is the same as for .NET 2.0.

Quote:
I think I can get your first suggestion to work. Is it possible to
declare a variable inside the lambda? I need something like:
(x =
string[] fields = x.Split();
new { name = fields[0], company = fields[1];
)
but I'm getting compiler errors.

I appologize for taking up your time -- I clearly have a lot of reading to
do before I've got this down.
Can I give a quick shameless plug?

http://manning.com/skeet

And there's no need to apologise - if I didn't want to take time
helping people, I wouldn't be here

--
Jon Skeet - <skeet (AT) pobox (DOT) com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.