字典检查

本文关键字:检查 字典 | 更新日期: 2023-09-27 18:06:13

我有一个列表,我在foreach循环中获取列表中的每个项目。

    private void saveButton_Click(object sender, EventArgs e)
    {
            if (saveFile1.ShowDialog() == DialogResult.OK)
            {
                StreamWriter sw = new StreamWriter(saveFile1.FileName);
                int i = 1;
                Dictionary<string, int> dictionary = new Dictionary<string, int>();
                foreach (var line in theFinalList)
                {
                    //compareList.Add(line.PartNumber[]);
                    if (!line.Name.Contains("FID") || !line.PartNumber.Contains("FIDUCIAL") ||
                        !line.PartNumber.Contains("EXCLUDES") || !line.PartNumber.Contains("excludes"))
                    {
                        //This doesn't work obviously.. :/
                        sw.WriteLine("( {0} 0 1 1 0 0 0 0 '"{1}'" '"{2}'" '"'" {3} 0 0 0 0 0 )",
                            i, line.PartDescription, line.PartNumber, partNumberOccurrences);
                    }
                    i++;
                }
            }

并且我在使用字典将行的每个部分存储在字典中并在发现重复时增加itemCounter时遇到麻烦。我正在尝试做这样的事情,但我不明白它的逻辑。

我想只输出partNumber 1的时间和它出现的次数,以及与partNumber同一行的对应值。

输出如下所示:

( 1 0 1 1 0 0 0 0 "2125R_1.0" "136380" "" 18 0 0 0 0 0 )
( 2 0 1 1 0 0 0 0 "2125R_1.0" "128587" "" 41 0 0 0 0 0 )
( 3 0 1 1 0 0 0 0 "2125R_1.0" "138409" "" 11 0 0 0 0 0 )
( 4 0 1 1 0 0 0 0 "2125R_1.0" "110984" "" 8 0 0 0 0 0 )
( 5 0 1 1 0 0 0 0 "3216R_1.3" "114441" "" 6 0 0 0 0 0 )
( 6 0 1 1 0 0 0 0 "3216C_1.3" "2006722" "" 16 0 0 0 0 0 )
( 7 0 1 1 0 0 0 0 "3216C_1.3" "135732" "" 6 0 0 0 0 0 )

谁能帮我创建一个字典来做这件事?

字典检查

您也可以为您的项目创建一个新类,并存储这些项目的列表,然后使用Linq对结果进行分组-您可能会或可能不会发现它更合乎逻辑:

public class MyItem
{
    public string Name { get; set; }
    public string PartNumber { get; set; } // I did this as string rather than int
    public string Description { get; set; }
}
public List<MyItem> items = new List<MyItem>();

然后,您似乎想要对项目进行分组(本例使用Linq并假设您已经填充了您的项目列表):

IEnumerable<IGrouping<int, MyItem>> grouped = items
    .GroupBy(t => Convert.ToInt32(t.PartNumber))
    .OrderBy(t => t.Key);
foreach(var item in grouped)
{
    Console.WriteLine("Number of Occurances:" + item.Key);
    Console.WriteLine("Name: " + item.ElementAt(0).Name);
    Console.WriteLine("Description: " + item.ElementAt(0).Description);
    Console.WriteLine("'n --------------------------------- 'n");
}

此示例假设具有相同部件号的所有部件将具有相同的名称和描述。

我想你需要这样的东西:

int value;
if (!dictionary.TryGetValue(line.PartNumber, out value))
{
    value = 0;
}
dictionary[line.PartNumber] = value + 1;

您还可以将匿名类型与linq一起使用,以提供更简洁的解决方案。下面是一个示例,假设您的行是制表符分隔的。

        var lines = new[]
                        {
                            "R80't123456'tsomething here1",
                            "R81't123456'tsomething here1",
                            "R81't123456'tsomething here1",
                        };
        var items = lines.Select(i => i.Split(''t')).Select(i => new {Name = i[0], PartNumber = i[1], Description = i[2]});
        var outLines =
            items.GroupBy(i => i.Name).Select(
                i =>
                string.Format("{0}'t{1}'t{2}'t{3}", i.First().Name, i.First().PartNumber, i.First().Description,
                              i.Count()));
        foreach (var outLine in outLines)
        {
            Console.WriteLine(outLine);
        }
        Console.Read();
foreach (string word in lineList)
{
    if (dictionary.ContainsKey(word)
        dictionary[word]++;
    else
        dictionary[word] = 1;
}