简体   繁体   English

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

[英]What C# Data Structure Should I Use?

This question pertains to C#, LINQ grouping and Collections.此问题与 C#、LINQ 分组和 Collections 相关。

I'm currently working on a grouping issue and I wanted to get some feedback from the community.我目前正在处理一个分组问题,我想从社区获得一些反馈。 I've encountered this particular problem enough times in the past that I'm thinking of writing my own data structure for it.过去我已经多次遇到这个特殊问题,我正在考虑为它编写自己的数据结构。 Here are the details:以下是详细信息:

Suppose you have a grouping that consists of a manufacturer and products and the data structure is grouped by manufacturer.假设您有一个由制造商和产品组成的分组,并且数据结构按制造商分组。 There are many manufacturers and many products.有许多制造商和许多产品。 The manufacturers each have a unique name and id.每个制造商都有一个唯一的名称和 ID。 The products may have similar names, but they do have unique ids.这些产品可能具有相似的名称,但它们确实具有唯一的 ID。 The proceeding list represents an example.进程列表代表一个例子。

Ford 1, Fiesta 1945, Taurus 6413, Fusion 4716, F1 8749,福特 1、嘉年华 1945、金牛座 6413、Fusion 4716、F1 8749、

Toyota 2, Camry 1311, Prius 6415, Corolla 1117, Tacoma 9471丰田 2、凯美瑞 1311、普锐斯 6415、卡罗拉 1117、塔科马 9471

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

etc... ETC...

The data structure I would use to represent this would be我用来表示这一点的数据结构是

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

but this doesn't exist.但这不存在。 So my question I want to ask the community is what data structure would you recommend and why?所以我想问社区的问题是你会推荐什么数据结构,为什么?

Update:更新:

I would like to keep the types anonymous and avoid the dynamic keyword.我想保持类型匿名并避免使用动态关键字。 So the data structure would be something like所以数据结构就像

IEnumerable SomeDataStructure<T, U, V>

The other requirement is that is can have duplicated items.另一个要求是可以有重复的项目。 Here's kind of what I'm thinking:这是我的想法:

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

Update:更新:

I decided to go with a Tuple data structure.我决定使用元组数据结构 go。 It's very powerful and easy to query against.它非常强大且易于查询。 The proceding code is how I ended up using it to create my manufacturer-vehicle relationships.处理代码是我最终使用它来创建制造商-车辆关系的方式。 The result is a nicely ordered data structure that has unique manufacturers ordered by name with their associated unique vehicles ordered by name.结果是一个有序的数据结构,其中包含按名称排序的唯一制造商以及按名称排序的相关唯一车辆。

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

I would create a class named Manufacturer:我将创建一个名为制造商的 class:

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

Then create a Product class:然后创建一个产品 class:

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

Then use LINQ projection with the Select extension method to create the Manufacturer object.然后使用 LINQ 投影和 Select 扩展方法创建制造商 object。

Like this?像这样?

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

EDIT: If you want to use anonymous types (as you mentioned now in your update) then GroupBy should work just as well if you construct anonymous objects, instead of declaring a Manufacturer and Product class as in my sample.编辑:如果您想使用匿名类型(正如您现在在更新中提到的那样),那么如果您构造匿名对象, GroupBy应该也能正常工作,而不是像我的示例中那样声明ManufacturerProduct class。

MyDataStructure sounds very similar to a Tuple . MyDataStructure听起来与Tuple非常相似。 See here for the three-generic parameter variant.有关三通用参数变体,请参见此处 Tuple gives a strongly typed container for a number of specified other types. Tuple为许多指定的其他类型提供了一个强类型容器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM