Expression.Condition(nullableType.HasValue, new classInstance(){ ... }, null) 可以通过其他方式完成吗?

发布于 2024-09-29 13:37:18 字数 2711 浏览 0 评论 0原文

我正在开发一个投影实用程序,并且还有最后一个(更多?)障碍需要清除...

场景如下:

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int? AddressID { get; set; }
    public Address Address { get; set; }
    public string Otherproperty1 { get; set; }
    public string Otherproperty2 { get; set; }
    public string Otherproperty3 { get; set; }
    public string Otherproperty4 { get; set; }
}

public class PersonSummary
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int? AddressID { get; set; }
    public AddressSummary Address { get; set; }

}

public class Address
{
    public int AddressID { get; set; }
    public string HouseNumber { get; set; }
    public string StreetName { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public int Zip { get; set; }
    public string Otherproperty1 { get; set; }
    public string Otherproperty2 { get; set; }
    public string Otherproperty3 { get; set; }
    public string Otherproperty4 { get; set; }
}

public class AddressSummary
{
    public int AddressID { get; set; }
    public string HouseNumber { get; set; }
    public string StreetName { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public int Zip { get; set; }
}

我成功地让我的实用程序按如下方式工作:

ProjectionUtility.Project<Person,PersonSummary>();

它将返回此表达式 Lambda:

p => new PersonSummary(){
    FirstName = p.FirstName,
    LastName = p.LastName,
    AddressID = p.AddressID,
    Address = p.AddressID.HasValue
        ? new AddressSummary(){ AddressID = p.Address.AddressID, HouseNumber = p.Address.HouseNumber, etc... }
        : new AddressSummary(){}
}

我的目标是能够如果 AddressID.HasValue 为 false,则设置 Address = null 如下所示:

p => new PersonSummary(){
    FirstName = p.FirstName,
    LastName = p.LastName,
    AddressID = p.AddressID,
    Address = p.AddressID.HasValue
        ? new AddressSummary(){ AddressID = p.Address.AddressID, HouseNumber = p.Address.HouseNumber, etc... }
        : null
}

这很容易“手动”完成,但是当我尝试使用表达式以编程方式创建此 lambda 时,我陷入困境......

我目前正在使用本质上分解为(我知道语法不正确,但我这样做是为了展示我在做什么):

Expression.Condition(
    p.AddressID.HasValue,
    new AddressSummary(){},
    new AddressSummary(){});

如果我尝试这样做:

Expression.Condition(
    p.AddressID.HasValue,
    new AddressSummary(){},
    null);

我不能,因为 iftrue 和 iffalse 必须是相同的类型(AddressSummary) 所以我现在陷入了为 iffalse 参数创建一个新的 AddressSummary 的困境。

有什么想法吗?

谢谢!

I am working on a projection utility and have one last (more?) hurdle to clear...

Here is the scenario:

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int? AddressID { get; set; }
    public Address Address { get; set; }
    public string Otherproperty1 { get; set; }
    public string Otherproperty2 { get; set; }
    public string Otherproperty3 { get; set; }
    public string Otherproperty4 { get; set; }
}

public class PersonSummary
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int? AddressID { get; set; }
    public AddressSummary Address { get; set; }

}

public class Address
{
    public int AddressID { get; set; }
    public string HouseNumber { get; set; }
    public string StreetName { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public int Zip { get; set; }
    public string Otherproperty1 { get; set; }
    public string Otherproperty2 { get; set; }
    public string Otherproperty3 { get; set; }
    public string Otherproperty4 { get; set; }
}

public class AddressSummary
{
    public int AddressID { get; set; }
    public string HouseNumber { get; set; }
    public string StreetName { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public int Zip { get; set; }
}

I successfully have my utility working something like this:

ProjectionUtility.Project<Person,PersonSummary>();

Which will return this Expression Lambda:

p => new PersonSummary(){
    FirstName = p.FirstName,
    LastName = p.LastName,
    AddressID = p.AddressID,
    Address = p.AddressID.HasValue
        ? new AddressSummary(){ AddressID = p.Address.AddressID, HouseNumber = p.Address.HouseNumber, etc... }
        : new AddressSummary(){}
}

My goal is to be able to set Address = null if the AddressID.HasValue is false like so:

p => new PersonSummary(){
    FirstName = p.FirstName,
    LastName = p.LastName,
    AddressID = p.AddressID,
    Address = p.AddressID.HasValue
        ? new AddressSummary(){ AddressID = p.Address.AddressID, HouseNumber = p.Address.HouseNumber, etc... }
        : null
}

This is easily done "manually", however when I try to programatically create this lambda using expressions, I'm stuck...

I am currently using what essentially breaks down to this (I know the syntax isn't right, but I did it this way to in an attempt to show what it is I'm doing):

Expression.Condition(
    p.AddressID.HasValue,
    new AddressSummary(){},
    new AddressSummary(){});

If I try this:

Expression.Condition(
    p.AddressID.HasValue,
    new AddressSummary(){},
    null);

I can't because iftrue and iffalse must be the same type (AddressSummary) so I am stuck at the moment creating a new AddressSummary for the iffalse argument.

Any ideas?

Thanks!

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

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

发布评论

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

评论(1

雨后咖啡店 2024-10-06 13:37:18

您需要将 null 转换为正确的类型。在本例中:

Expression.Condition(
    p.AddressID.HasValue,
    new AddressSummary(){},
    (AddressSummary)null);

null 可以是任何(引用)类型,但默认情况下它是 System.Object 类型。如果你想要它是另一种类型,你必须告诉它。

You need to cast your null to the correct type. In this case:

Expression.Condition(
    p.AddressID.HasValue,
    new AddressSummary(){},
    (AddressSummary)null);

null can be any (reference) type, but by default it is of type System.Object. If you want it to be another type, you have to tell it.

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