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

发布于 2024-11-29 05:45:21 字数 2832 浏览 0 评论 0原文

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

我目前正在研究分组问题,我想从社区获得一些反馈。过去我已经多次遇到这个特殊问题,所以我正在考虑为其编写自己的数据结构。详细信息如下:

假设您有一个由制造商和产品组成的分组,并且数据结构按制造商分组。生产厂家很多,产品也很多。每个制造商都有唯一的名称和 ID。这些产品可能具有相似的名称,但它们确实具有唯一的 ID。前面的列表代表了一个示例。

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

Toyota 2, Camry 1311, Prius 6415, Corolla 1117, Tacoma 9471

Chevrolet 3, Silverado 4746, Camero 6473, Volt 3334, Tahoe 9974

等...

数据结构我会用它来表示这将是

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

,但它不存在。所以我想问社区的问题是你们会推荐什么数据结构,为什么?

更新:

我想保持类型匿名并避免使用动态关键字。因此数据结构类似于

IEnumerable SomeDataStructure

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

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

更新:

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

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

This question pertains to C#, LINQ grouping and 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. The products may have similar names, but they do have unique ids. The proceeding list represents an example.

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

Toyota 2, Camry 1311, Prius 6415, Corolla 1117, Tacoma 9471

Chevrolet 3, Silverado 4746, Camero 6473, Volt 3334, Tahoe 9974

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝海似她心 2024-12-06 05:45:21

我将创建一个名为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;}
}

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

I would create a class named Manufacturer:

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

Then create a Product 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.

近箐 2024-12-06 05:45:21

像这样?

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 应该同样有效,而不是声明 ManufacturerProduct 类,如我的示例中所示。

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.

雪落纷纷 2024-12-06 05:45:21

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

MyDataStructure sounds very similar to a Tuple. See here for the three-generic parameter variant. Tuple gives a strongly typed container for a number of specified other types.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文