Reputation: 1665
I have this:
if (Folder == "Unprocessed")
{
var FolderEmails = from emails in EmailManagerDAL.Context.Emails
join activities in EmailManagerDAL.Context.EmailActivities on emails.ID equals activities.EmailID
where activities.EmailID != null
select new { emails.ID, emails.MessageFrom,emails.MessageSubject, emails.MessageDeliveryTime };
}
else // Processed
{
}
I need to move the FolderEmails var above the "if" but am having numerous difficulties.
Of course setting it to null initially is illegal.
I've tried stuff like this that also does not work:
var FolderEmails = new { ID = new int(), MessageFrom = string.Empty, MessageSubject = string.Empty, MessageDeliverTime = new DateTime() };
Could really use some help on this.
Thanks!
Upvotes: 1
Views: 5541
Reputation: 1396
I know this is reframing the question a bit, do you need the anonymous type? Could you create a class to hold the same information, then create some sort of IEnumerable container instead of the var?
List<Foo> FolderEmails;
if (Folder == "Unprocessed")
{
FolderEmails = from emails in EmailManagerDAL.Context.Emails
join activities in EmailManagerDAL.Context.EmailActivities on emails.ID equals activities.EmailID
where activities.EmailID != null
select new Foo {
ID = emails.ID,
MessageFrom = emails.MessageFrom,
MessageSubject = emails.MessageSubject,
MessageDeliverTime = emails.MessageDeliveryTime
};
}
else // Processed
{
}
The thought being if you need the type to live outside of that scope, it might be worth creating it.
Upvotes: 1
Reputation: 7631
I don't think anonymous types are intended to be used like you're wanting (the compiler creates the anonymous type based on the assignment result). Why don't you just declare a Tuple type and use it.
Edit:
IEnumerable<Tuple<int, string, string, DateTime>> FolderEmails =
default( IEnumerable<Tuple<int,string,string,DateTime>> );
if (folder == "Unprocessed")
{
FolderEmails = from emails in EmailManagerDAL.Context.Emails
join activities in EmailManagerDAL.Context.EmailActivities on emails.ID equals activities.EmailID
where activities.EmailID != null
select new Tuple<int, string, string, DateTime>
(
emails.ID,
emails.MessageFrom,
emails.MessageSubject,
emails.MessageDeliveryTime
);
}
else // Processed
{
}
You need to change the "select new { field = value }" because it's no longer an anonymous type being created, it's a locally declared explicit type of:
Tuple<int,string,string,DateTime>
which are immutable value types so you'll have to call the constructor to create them ( select new Tuple( values ) ).
Upvotes: 1
Reputation: 604
Sorry but the correct answer here is you can´t. The C# 3.0 specifications describe anonymous types as tuple types automatically inferred and created from object initializers. An object initializer specifies values from one or more fields or properties of an object. That means you specify a set of properties for an object through a series of assignments and an object is assigned these properties.
In your case the compiler is inferred a new object type to FolderEmails here:
var FolderEmails = new { ID = new int(), MessageFrom = string.Empty, MessageSubject = string.Empty, MessageDeliverTime = new DateTime() };
but this type of object could be not the same as here:
select new { emails.ID, emails.MessageFrom,emails.MessageSubject, emails.MessageDeliveryTime };
So this is the reason why it doesn´t allow you to perform this operation. In this case, if you wanna extract this var from the if block, you need to create an explicity object to use it. You can only use FolderEmails's getters and setters after initialize it.
Upvotes: 1