如何选择的.Net类型的对象用C#的名单上一个对象的属性的所有值 [英] How to select all the values of an object's property on a list of typed objects in .Net with C#

查看:154
本文介绍了如何选择的.Net类型的对象用C#的名单上一个对象的属性的所有值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

唉,我怎么解释这个...也许一个简单的问题,但我的心是油炸。

Ugh, how do I explain this one... Probably a simple question but my mind is fried.

假设我有这个类:

public class NestedObject
{
    public string NestedName { get; set; }
    public int NestedIntValue { get; set; }
    public decimal NestedDecimalValue { get; set; }
}

public class SomeBigExternalDTO
{
    public int Id { get; set; }
    public int UserId { get; set; }
    public int SomeIntValue { get; set; }
    public long SomeLongValue { get; set; }
    public decimal SomeDecimalValue { get; set; }
    public string SomeStringValue { get; set; }
    public NestedObject SomeNestedObject { get; set; }
    // ... thousands more of these properties... inherited code
}

和我想填充类是在这里:

And the class I'd like to populate is here:

public class MyResult
{
    public int UserId { get; set; }  // user id from above object
    public string ResultValue { get; set; }  // one of the value fields from above with .ToString() executed on it
}

我想要做的就是创建一个帮手返回此对象的列表中的所有实例的属性值(横截面是我能形容它的最好方式,我猜):

What I'd like to do is create a helper to return the property values (a cross section is the best way I could describe it I guess) of all instances in a list of this object:

var foo = new List<SomeBigExternalDTO>();
foo = GetMyListOfSomeBigExternalDTO();

public static List<MyResult> AwesomeHelper(List<SomeBigExternalDTO> input, SearchableProperty thePropertyIWant)
{
    // some magic needs to happen here...
}

在这里棘手的部分是我想基于链路选择在属性动态传递(我不知道如何做到这一点):

The tricky part here is I want to dynamically pass in the property based on a link selector (I have no clue how to do this):

var output = AwesomeHelper(GetMyListOfSomeBigExternalDTO(), x => x.SomeIntValue);
var output2 = AwesomeHelper(GetMyListOfSomeBigExternalDTO(), x => x.SomeNestedObject.NestedIntValue); 

这应该返回与用户名和SomeIntValue.ToString(myResult中对象的列表),对应于每个项目中的输入列表中。

And this should return a list of MyResult objects with the UserId and SomeIntValue.ToString() corresponding to each item in the input list.

哇,我真的希望这是有道理的。请让我知道这是不明确的,我会提供更多细节。我真的希望这是一件烤成,我已经忽略了图书馆。

Wow, I really hope this makes sense. Please let me know if this is not clear I'll provide more details. I'm really hoping this is something baked into the libraries that I've overlooked.

这是我的任何想法会做到这一点?

Any ideas on I'd accomplish this?

推荐答案

试图创建一个通用的列表操作符,你最终重新实现什么LINQ已经为您提供了通常当。

Often when trying to create a general purpose list operator you end up reimplementing what LINQ already offers you.

这里的LINQ code为你以后有什么(没有 AwesomeHelper 函数):

Here's the LINQ code for what you're after (without an AwesomeHelper function):

var results = list.Select(l => new MyResult()
{
    UserId = l.UserId,
    ResultValue = l.SomeDecimalValue.ToString()
}).ToList();

很简单。

如果你想有一个 AwesomeHelper 功能,你要求那么它看起来是这样的:

If you want to have an AwesomeHelper function as you requested then it looks like this:

public static List<MyResult> AwesomeHelper(
    List<SomeBigExternalDTO> input,
    Func<SomeBigExternalDTO, object> selector)
{
    return input
        .Select(i => new MyResult()
        {
            UserId = i.UserId,
            ResultValue = selector(i).ToString()
        })
        .ToList();
}

和调用code是这样的:

And the calling code look like this:

var results = AwesomeHelper(list, x => x.SomeIntValue);

要我,虽然,这是现在的可读比LINQ选项。现在有一些神奇的被造成,很难制定出什么。

To me, though, this is now less readable than the LINQ option. Now there is some magic being wrought and it's hard to work out what.

我有一个替代方案,这将使你的两全其美。

I have an alternative that will give you the best of both worlds.

首先,定义了一个名为扩展方法 ToMyResult 映射一个 SomeBigExternalDTO 实例到一个单一的 myResult中与字段选择器,像这样的:

First, define an extension method called ToMyResult that maps a single SomeBigExternalDTO instance into a single MyResult with a field selector, like this:

public static class AwesomeHelperEx
{
    public static MyResult ToMyResult(
            this SomeBigExternalDTO input,
            Func<SomeBigExternalDTO, object> selector)
    {
        return new MyResult()
        {
            UserId = input.UserId,
            ResultValue = selector(input).ToString()
        };
    }
}

现在调用code是晶莹剔透的,灵活的和简洁。在这里,它是:

Now the calling code is crystal clear, flexible and concise. Here it is:

var results = (
        from item in list
        select item.ToMyResult(x => x.SomeLongValue)
    ).ToList();

我希望这有助于。

I hope this helps.

这篇关于如何选择的.Net类型的对象用C#的名单上一个对象的属性的所有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆