使用 LINQ 和匿名类型初始化 var

发布于 2024-11-14 06:14:35 字数 767 浏览 4 评论 0原文

我有这个:

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
{

}

我需要将FolderEmails var移到“if”上方,但遇到很多困难。

当然,最初将其设置为 null 是非法的。

我已经尝试过类似的方法,但也不起作用:

var FolderEmails = new { ID = new int(), MessageFrom = string.Empty, MessageSubject = string.Empty, MessageDeliverTime = new DateTime() };

确实可以在这方面使用一些帮助。

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

离去的眼神 2024-11-21 06:14:35

我不认为匿名类型应该像您想要的那样使用(编译器根据赋值结果创建匿名类型)。为什么不直接声明一个 Tuple 类型并使用它呢?

编辑:

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
{

}

您需要更改“select new { field = value }”,因为它不再是创建的匿名类型,而是本地声明的显式类型:

Tuple<int,string,string,DateTime>

它们是不可变的值类型,因此您必须调用构造函数来创建他们(选择新的元组(值))。

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 ) ).

大姐,你呐 2024-11-21 06:14:35

抱歉,但正确的答案是你不能。 C# 3.0 规范将匿名类型描述为从对象初始值设定项自动推断和创建的元组类型。对象初始值设定项指定对象的一个​​或多个字段或属性的值。这意味着您通过一系列赋值为对象指定一组属性,并为对象分配这些属性。

在您的情况下,编译器会在这里推断出一个新的对象类型为FolderEmails:

var FolderEmails = new { ID = new int(), MessageFrom = string.Empty, MessageSubject = string.Empty, MessageDeliverTime = new DateTime() };

但是这种类型的对象可能与这里不同:

select new { emails.ID, emails.MessageFrom,emails.MessageSubject, emails.MessageDeliveryTime };

所以这就是为什么它不允许您执行此操作的原因。
在这种情况下,如果你想从 if 块中提取这个 var,你需要创建一个显式对象来使用它。
您只能在初始化后使用FolderEmails 的getter 和setter。

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.

桜花祭 2024-11-21 06:14:35

我知道这有点重新定义了问题,你需要匿名类型吗?您可以创建一个类来保存相同的信息,然后创建某种 IEnumerable 容器而不是 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
{

}

想法是,如果您需要该类型存在于该范围之外,那么可能值得创建它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文