变换一个List<对象>字符串,排序列表<匿名类型字典&LT的日期时间,双>> [英] Transform a List<object> of anonymous type to a Dictionary<string, SortedList<DateTime, double>>

查看:141
本文介绍了变换一个List<对象>字符串,排序列表<匿名类型字典&LT的日期时间,双>>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个创建一个本地列表℃的功能;对象> ,其中的对象是一个匿名类型。我需要返回这些结果解释<字符串,排序列表<日期时间,双>>



数据是列表如下。

  {安全性=6752 JT,日期= {1/17/2011 12:00:00 AM},zScore = 1} 
{安全=6753 JT,日期= {1/17/2011 12:00 :00},zScore = 2}
{安全=6754 JT,日期= {1/17/2011 12:00:00 AM},zScore = 3}
{安全性=6752 JT,日期= {2011年1月18日12:00:00 AM},zScore = 1}
{安全=6753 JT,日期= {2011年1月18日12:00:00 AM} ,zScore = 2}
{安全=6754 JT,日期= {2011年1月18日12:00:00 AM},zScore = 3}
{安全=6752 JT,日期= {2011年1月19日12:00:00 AM},zScore = 1}
{安全=6753 JT,日期= {2011年1月19日12:00:00 AM},zScore = 2 }
{安全=6754 JT,日期= {2011年1月19日12:00:00 AM},zScore = 3}

我想使用LINQ这些结果放在一个词典<字符串,排序列表<日期时间,双>> 其中,字典的关键是安全,值是一个包含安全的所有日期/ z得分值的排序列表。



我能做到这一点的LINQ当它是自定义对象,但你如何使用匿名类型的对象做



注:该查询最初发布在不必要的复杂,措辞不佳方式。这可能是为什么没有人回答了这个问题!我包括如果你想看到为什么输出是这种形式的链接。的结果
C#LINQ Z得分查询输出到字典<字符串,排序列表<日期时间,双>>


解决方案

所以基本上你问如何从对象拆箱一个匿名类型 <? / p>所有的

首先,我建议不要使用列表<对象> ,只是......创建一个自定义类。

 公共类SecurityScore {
公共字符串安全{搞定;组; }
公众的DateTime日期{搞定;组; }
公众诠释ZScore {搞定;组; }
}



不过,如果出于某种原因,你需要做到这一点,试试这个方法从乔恩斯基特:




我一直都知道,这是完全容易通过声明,该方法将返回返回匿名类型的实例目的。然而,这并没有发生,我今天之前,你实际上可以转换回该类型之后。当然,你不能只用一个正常的铸表达 - 这需要在编译时被称为该类型的名称。但是你可以做一个通用的方法铸造......,你可以使用类型推断提供一个类型参数...和两个匿名类型实例创建表达式将在同一装配中使用相同类型如果订单,名称和类型,属性是相同的。




如果你想探索自己的解决方案,看看他的blog~~MD~~aux帖子关于这个问题的。



有关完整性,我会在这里发布自己的代码:

 静态类GrottyHacks 
{
内部静态牛逼演员LT; T>(对象的目标,T为例)
{
回报率(T)的目标;
}
}

类CheesecakeFactory
{
静态对象CreateCheesecake()
{
返回新{水果=草莓唐培=巧克力};
}

静态无效的主要()
{
对象weaklyTyped = CreateCheesecake();
VAR stronglyTyped = GrottyHacks.Cast(weaklyTyped,
新的{水果=,唐培=});

Console.WriteLine(芝士蛋糕:{0}({1}),
stronglyTyped.Fruit,stronglyTyped.Topping);
}
}



我必须承认,虽然我真的不像拳击的想法/拆箱匿名类型,他的做法是相当真棒,且占用的代码相对较少的线路。



所以,现在我给你一个的可能的解决方案,我必须要问 - 你为什么做这种方式,而不是创建一个简单的类



编辑:同时完整性,这里是我将如何实现你的尤其的使用乔恩斯基特的解决方案问题:

 无效的主要()
{
//创建(盒装)的匿名对象
变种securitiesBoxed =新名单℃的列表;对象>(){
新{安全=6752 JT,日期= DateTime.Parse(1/17/2011 12:00:00 AM),zScore = 1},
新的{安全性=6753 JT,日期= DateTime.Parse (1/17/2011 12:00:00 AM),zScore = 2},
新的{安全性=6754 JT,日期= DateTime.Parse(1/17/2011 12:00:上午12点),zScore = 3},
新的{安全性=6752 JT,日期= DateTime.Parse(2011年1月18日上午12点00分零零秒),zScore = 1},
新的{安全性=6753 JT,日期= DateTime.Parse(2011年1月18日12:00:00 AM),zScore = 2},
新的{安全性=6754 JT ,日期= DateTime.Parse(2011年1月18日上午12时00分○○秒),zScore = 3},
新的{安全性=6752 JT,日期= DateTime.Parse(1/19 / 2011 12:00:00 AM),zScore = 1},
新的{安全性=6753 JT,日期= DateTime.Parse(2011年1月19日12:00:00 AM), zScore = 2},
新的{安全性=6754 JT,日期= DateTime.Parse(2011年1月19日12:00:00 AM),zScore = 3}
};

//现在,转换为词典<字符串,排序列表<日期时间,双>> ...
VAR securitiesUnboxed = securitiesBoxed.Select(X =>演员(X,新{安全=,日期=新的日期时间(),zScore = 0}))
.GroupBy(X => x.Security)
.ToDictionary(X => x.Key, X => x.OrderBy(Y => y.Date));
}

//这是将铸就我们的匿名类型
内部静态牛逼演员LT静态方法; T>(对象的目标,T为例)
{
返回(T)的目标;
}

LINQPad ,上面的代码会导致以下数据:




I have a function that creates a local List<object> where the object is an anonymous type. I need to return these results in Dictionary<string, SortedList<DateTime, double>>.

The data is the the list looks like this.

{ Security = "6752 JT", Date = {1/17/2011 12:00:00 AM}, zScore = 1 }
{ Security = "6753 JT", Date = {1/17/2011 12:00:00 AM}, zScore = 2 }
{ Security = "6754 JT", Date = {1/17/2011 12:00:00 AM}, zScore = 3 }
{ Security = "6752 JT", Date = {1/18/2011 12:00:00 AM}, zScore = 1 }
{ Security = "6753 JT", Date = {1/18/2011 12:00:00 AM}, zScore = 2 }
{ Security = "6754 JT", Date = {1/18/2011 12:00:00 AM}, zScore = 3 }
{ Security = "6752 JT", Date = {1/19/2011 12:00:00 AM}, zScore = 1 }
{ Security = "6753 JT", Date = {1/19/2011 12:00:00 AM}, zScore = 2 }
{ Security = "6754 JT", Date = {1/19/2011 12:00:00 AM}, zScore = 3 }

I would like to use LINQ to place these results into a Dictionary<string, SortedList<DateTime, double>> where the dictionary's key is the Security, and the value is a SortedList containing all the date/z-score values for the security.

I can do this in LINQ when it is a custom object, but how do you do it with an anonymous type object?

Note: This query was originally posted in an unnecessarily complicated, and poorly phrased way. Which is probably why no one answered it! I'm including the link in case you wanted to see why the output is in this form.
C# LINQ Z-Score query output to a Dictionary<string, SortedList<DateTime, double>>

解决方案

So basically you're asking how to unbox an anonymous type from object?

First of all, I recommend not using a List<object> and just ... creating a custom class.

public class SecurityScore {
    public string Security { get; set; }
    public DateTime Date { get; set; }
    public int ZScore { get; set; }
}

However, if for whatever reason you need to do this, try this approach from Jon Skeet:

I've always known that it's perfectly easy to return an instance of an anonymous type by declaring that the method will return object. However, it hadn't occurred to me before today that you can actually cast back to that type afterwards. Of course, you can't just use a normal cast expression - that requires the name of the type to be known at compile-time. But you can do a cast in a generic method... and you can use type inference to supply a type argument... and two anonymous type instance creation expressions will use the same type within the same assembly if the order, names and types of the properties are the same.

If you want to explore his solution, check out his blog post on the subject.

For completeness, I will post his code here:

static class GrottyHacks
{
    internal static T Cast<T>(object target, T example)
    {
        return (T) target;
    }
}

class CheesecakeFactory
{
    static object CreateCheesecake()
    {
        return new { Fruit="Strawberry", Topping="Chocolate" };
    }

    static void Main()
    {
        object weaklyTyped = CreateCheesecake();
        var stronglyTyped = GrottyHacks.Cast(weaklyTyped,
            new { Fruit="", Topping="" });

        Console.WriteLine("Cheesecake: {0} ({1})",
            stronglyTyped.Fruit, stronglyTyped.Topping);            
    }
}

I must admit that, while I don't really like the idea of boxing/unboxing an anonymous type, his approach is pretty awesome, and takes up relatively few lines of code.

So, now that I've given you a possible solution, I must ask -- why are you doing it this way, as opposed to creating a simple class?

Edit: Also for completeness, here's how I would implement your particular problem using Jon Skeet's solution:

void Main()
{
    // Create a list of (boxed) anonymous objects
    var securitiesBoxed = new List<object>() {
        new { Security = "6752 JT", Date = DateTime.Parse("1/17/2011 12:00:00 AM"), zScore = 1 },
        new { Security = "6753 JT", Date = DateTime.Parse("1/17/2011 12:00:00 AM"), zScore = 2 },
        new { Security = "6754 JT", Date = DateTime.Parse("1/17/2011 12:00:00 AM"), zScore = 3 },
        new { Security = "6752 JT", Date = DateTime.Parse("1/18/2011 12:00:00 AM"), zScore = 1 },
        new { Security = "6753 JT", Date = DateTime.Parse("1/18/2011 12:00:00 AM"), zScore = 2 },
        new { Security = "6754 JT", Date = DateTime.Parse("1/18/2011 12:00:00 AM"), zScore = 3 },
        new { Security = "6752 JT", Date = DateTime.Parse("1/19/2011 12:00:00 AM"), zScore = 1 },
        new { Security = "6753 JT", Date = DateTime.Parse("1/19/2011 12:00:00 AM"), zScore = 2 },
        new { Security = "6754 JT", Date = DateTime.Parse("1/19/2011 12:00:00 AM"), zScore = 3 }
    };

    // Now, to convert to a Dictionary<string, SortedList<DateTime, double>>...
    var securitiesUnboxed = securitiesBoxed.Select(x => Cast(x, new { Security = "", Date = new DateTime(), zScore = 0 }))
        .GroupBy(x => x.Security)
        .ToDictionary(x => x.Key, x => x.OrderBy(y => y.Date));
}

// This is the static method that will cast our anonymous type
internal static T Cast<T>(object target, T example)
{
    return (T) target;
}

In LINQPad, the above code results in the following data:

这篇关于变换一个List&LT;对象&gt;字符串,排序列表&LT;匿名类型字典&LT的日期时间,双&GT;&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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