Dictionary Checking
I have a list and I am getting each item in the list in a foreach
loop.
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("EXCLUDE开发者_如何学PythonS") || !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++;
}
}
And I am having trouble using a dictionary to store each part of the line in a dictionary and to increment the the itemCounter whenever it is found as duplicate. I am trying to do something like this, but I cannot figure out the logic for it.
And I would like to only output the partNumber 1 time and also the amount of times it occurred as well as a corresponding value on the same line as the partNumber.
The output would look like this:
( 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 )
Can anyone help me with creating a dictionary to do this?
You could also create a new class for your items and store a List of these items and then use Linq to group the results - you may or may not find it more logical:
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>();
Then, you seem to want to be grouping the items (this example uses Linq and assumes you have populated your items List):
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");
}
This example assumes that everything with the same part number will have the same name and description.
I think you want something like this:
int value;
if (!dictionary.TryGetValue(line.PartNumber, out value))
{
value = 0;
}
dictionary[line.PartNumber] = value + 1;
You could also use anonymous types along with linq to give you a neater solution. Here's an example assuming that your lines are tab separated.
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;
}
精彩评论