开发者

How to select all the values of an object's property on a list of typed objects in .Net with C#

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

Suppose I have this class:

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 SomeIntV开发者_开发百科alue { 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); 

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?


You could implement it as an extension method:

public static IEnumerable<MyResult> AwesomeHelper(this IEnumerable<SomeBigExternalDTO> input, 
                                                  Func<SomeBigExternalDTO, int> intMapper)
{
    foreach (var item in input)
        yield return new MyResult() 
        {
            UserId = item.UserId, 
            ResultValue = intMapper(item) 
        };
}

Now you can use it like this:

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

Having said that - dont' do that - it somehow looks like you are reinventing what Linq already offers you, you can do just the same using only Linq:

var output = GetMyListOfSomeBigExternalDTO().Select( x=> new MyResult()
             {
                UserId = item.UserId, 
                ResultValue = x.SomeIntValue
             });

var output2 = GetMyListOfSomeBigExternalDTO().Select( x=> new MyResult()
             {
                UserId = item.UserId, 
                ResultValue = x.SomeNestedObject.NestedIntValue
             });


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

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();

Fairly simple.

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();
}

And the calling code look like this:

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

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.

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()
        };
    }
}

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜