Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / January 2008

Tip: Looking for answers? Try searching our database.

new pattern needed to declare container for var

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ChrisA - 31 Dec 2007 02:04 GMT
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
Jon Skeet [C# MVP] - 31 Dec 2007 07:24 GMT
(This would be better off in the C# group, btw - the CLR is unaware of
anonymous types.)

>   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
[quoted text clipped - 9 lines]
>
> 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.

Signature

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

ChrisA - 31 Dec 2007 18:16 GMT
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
ChrisA - 31 Dec 2007 18:33 GMT
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.
Jon Skeet [C# MVP] - 01 Jan 2008 15:06 GMT
>   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.

>   I think I can get your first suggestion to work.  Is it possible to
> declare a variable inside the lambda?  I need something like:
[quoted text clipped - 6 lines]
>   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 :)

Signature

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


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.