我应该使用什么C#数据结构?

时间:2011-08-10 21:57:38

标签: c# .net linq data-structures collections

这个问题涉及C#,LINQ分组和集合。

我目前正在处理分组问题,我想从社区获得一些反馈。我过去曾经遇到过这个特殊问题,我正在考虑为它编写自己的数据结构。以下是详细信息:

假设您有一个由制造商和产品组成的分组,并且数据结构按制造商分组。有许多制造商和许多产品。每个制造商都有一个独特的名称和ID。产品可能具有相似的名称,但它们具有唯一的ID。进程列表代表了一个例子。

Ford 1,Fiesta 1945,Taurus 6413,Fusion 4716,F1 8749,

丰田2,凯美瑞1311,普锐斯6415,卡罗拉1117,塔科马9471

雪佛兰3,Silverado 4746,Camero 6473,Volt 3334,Tahoe 9974

等...

我用来表示这个的数据结构是

IEnumerable<Manufacturer, ManufacturerID, IEnumerable<Product, ProductID>>

但这并不存在。所以我的问题我想问社区你会推荐什么样的数据结构?为什么?

更新

我想保持匿名类型并避免使用dynamic关键字。所以数据结构就像

IEnumerable SomeDataStructure<T, U, V>

另一个要求是可以有重复的项目。这就是我所想的:

public class MyDataStructure<T, U, V>
{
    // make this like a list, not a dictionary
}

更新

我决定使用Tuple数据结构。它非常强大且易于查询。程序代码是我最终用它来创建我的制造商 - 车辆关系的方式。结果是一个精心排序的数据结构,其中有独特的制造商按名称排序,并按名称排列相关的独特车辆。

public class ManufacturersVehicles
{
    public int ManufacturerID { get; set; }
    public string ManufacturerName { get; set; }
    public int VehicleID { get; set; }
    public string VehicleName { get; set; }
}

// "data" actually comes from the database. I'm just creating a list to use a mock structure to query against.
var data = new List<ManufacturersVehicles>
{
    { ManufacturerID = 1, Manufacturer = "Ford", VehicleID = 1945, VehicleName = "Fiesta" },
    { ManufacturerID = 1, Manufacturer = "Ford", VehicleID = 6413, VehicleName = "Taurus" },
    { ManufacturerID = 1, Manufacturer = "Ford", VehicleID = 4716, VehicleName = "Fusion" },
    etc...
};

// Get a collection of unique manufacturers from the data collection and order it by the manufacturer's name.
var manufacturers = data.Select(x => new { ManufacturerID = x.ManufacturerID, ManufacturerName = x.ManufacturerName })
                        .Distinct()
                        .OrderBy(x => x.ManufacturerName)
                        .Select(x => Tuple.Create(x.ManufacturerID, x.ManufacturerName, new Dictionary<int, string>()))
                        .ToList();

// Add the manufacturer's vehicles to it's associated dictionary collection ordered by vehicle name.
foreach (var manufacturer in manufacturers)
{
    // Get the collection of unique vehicles ordered by name.
    var vehicles = _alertDetails.Where(x => x.ManufacturerID == manufacturer.Item1)
                                .Select(x => new { VehicleID = x.VehicleID, VehicleName = x.VehicleName })
                                .Distinct()
                                .OrderBy(x => x.VehicleName);

    foreach (var vehicle in vehicles)
    {
        manufacturer.Item3.Add(vehicle.VehicleID, vehicle.VehicleName);
    }
}

3 个答案:

答案 0 :(得分:6)

我会创建一个名为Manufacturer的类:

public class Manufacturer
{
    public int ManufacturerId { get; set;}
    public string Name { get; set; }
    public IEnumerable<Product> Products { get; set;}
}

然后创建一个Product类:

public class Product
{
    public int ProductId { get; set;}
    public string Name { get; set;}
}

然后使用带有Select扩展方法的LINQ投影来创建Manufacturer对象。

答案 1 :(得分:3)

喜欢这个吗?

public class Manufacturer : IEquatable<Manufacturer>
{
    public string Name { get; private set; }
    public int ID { get; private set; }
    // ...
}

public class Product
{
    public string Name { get; private set; }
    public int ID { get; private set; }
    // ...
}

// groups is of type IEnumerable<IGrouping<Manufacturer, Product>>

var groups = data.GroupBy(row => new Manufacturer(row), row => new Product(row));

编辑:如果你想使用匿名类型(正如你在更新中提到的那样),那么GroupBy如果你构造匿名对象也应该工作,而不是声明Manufacturer和{{ 1}}我的样本中的类。

答案 2 :(得分:2)

MyDataStructure听起来与Tuple非常相似。有关三通用参数变体,请参阅hereTuple为许多指定的其他类型提供了一个强类型容器。