gpt4 book ai didi

c# - 将匿名类型的 List 转换为 Dictionary>
转载 作者:行者123 更新时间:2023-11-30 13:24:17 25 4
gpt4 key购买 nike

我有一个创建本地 List<object> 的函数其中对象是匿名类型。我需要在 Dictionary<string, SortedList<DateTime, double>> 中返回这些结果.

数据是列表,看起来像这样。

{ 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 }

我想使用 LINQ 将这些结果放入 Dictionary<string, SortedList<DateTime, double>> 中其中字典的键是证券,值是一个 SortedList,其中包含证券的所有日期/z 分数值。

当它是自定义对象时,我可以在 LINQ 中执行此操作,但是如何使用匿名类型对象执行此操作?

注意:此查询最初是以一种不必要的复杂且措辞不佳的方式发布的。这可能就是为什么没有人回答它的原因!如果您想了解为什么输出采用这种形式,我会提供链接。
C# LINQ Z-Score query output to a Dictionary<string, SortedList<DateTime, double>>

最佳答案

所以基本上你问的是如何从 object 中拆箱匿名类型?

首先,我建议不要使用 List<object>并且只是......创建一个自定义类。

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

但是,如果出于某种原因您需要这样做,请尝试 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.

如果您想探索他的解决方案,请查看他的 blog post关于这个问题。

为了完整起见,我将在这里发布他的代码:

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

我必须承认,虽然我不太喜欢装箱/拆箱匿名类型的想法,但他的方法非常棒,并且占用的代码行相对较少。

那么,既然我已经为您提供了一个可能的解决方案,我必须要问——您为什么要这样做,而不是创建一个简单的类?

编辑:同样为了完整起见,以下是我将如何使用 Jon Skeet 的解决方案来实现您的特定问题:

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

LINQPad ,以上代码产生以下数据:

linqified data

关于c# - 将匿名类型的 List<object> 转换为 Dictionary<string, SortedList<DateTime, double>>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5480850/

25 4 0