我真的被卡住了,并且在这里发布解决方案的很多人都是非常棒的(恕我直言),我想我会看到你们中最好的人可以解决这个问题。
一些背景
我正在尝试创建一个列表,该列表必须仅包含特定序列中的不同项目。 (它是一个主键,因此必须是不同的(我没有把它作为主键,但我必须处理我给出的内容,你知道它是怎么回事。)
为了便于理解这一要求,请考虑从食谱书中创建一个独特的食谱步骤列表。我的问题是这些“食谱”的“烹饪”经常改变他们创作杰作的顺序。
例如:
正如你所知道的那样,“添加盐和胡椒......”不能是食谱3中的数字2,并且在食谱1和2中仍然是正确的顺序。
我认为如果我可以识别“违规”列表项并在其末尾添加句点,从而使其独一无二,那么这将作为解决方案。
如何在C#中给定一个数据集(由SQL查询获取),并按正确顺序复制并放入类型字符串列表中? LINQ在这里不是必需的,但如果它提供了解决方案,我不会害怕使用它。
具体代码(或psedo-code):
如果你的第一个问题是“告诉我你的工作”,请注意我已经完成了相当多的工作,代码通常很长。
我很乐意使用伪代码或使用我的数据集尝试您的代码。
我也很高兴阅读其他可能相关的解决方案。
谢谢,我期待看到您的解决方案。
- 编辑:如果你不发布代码,我开始得到人们不喜欢它的印象。 所以这里(我上面说的很长)。代码可以工作,但它不能解决问题。它按顺序返回一个不同的列表,没有重复。 (如果下面的格式不好请原谅)
public void GetNewRecipeItemsFromDB(string RequestedRecipeName)
{
string connString = string.Empty;
string strGetRecipeElements_Sql = "SQL that returns the dataset";
string connString = GetConnectionString();
using (SqlConnection conn = new SqlConnection(connString))
{
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = strGetRecipeElements_Sql;
SqlDataReader reader = null;
try
{
conn.Open();
SqlDataAdapter adapter = new SqlDataAdapter(strGetRecipeElements_Sql, conn);
DataSet RecipeItems = new DataSet();
adapter.Fill(RecipeItems, "RecipeItems");
reader = cmd.ExecuteReader();
List<string> RecipeItemList = new List<string>();
//Create an array with existing RecipeItems
int readerCount = 0;
while (reader.Read())
{
RecipeItems GSI = new RecipeItems();
GSI.RecipeItem = reader[0].ToString();
GSI.Sequence = Convert.ToInt32(reader[1].ToString());
GSI.Rank = Convert.ToInt32(reader[2].ToString());
RecipeItemList.Add(GSI.RecipeItem.ToString());
readerCount++;
}
string[] CurrentRecipeItemArray = new string[readerCount];
string[] UpdatedRecipeItemArray = new string[readerCount];
//RecipeItemList.Sort();
label1.Text = "";
textBox1.Text = "";
CurrentRecipeItemArray = RecipeItemList.ToArray();
for (int y = CurrentRecipeItemArray.Length - 1; y >= 0; y--)
{
textBoxDBOrginal.Text += CurrentRecipeItemArray[y].ToString() + Environment.NewLine;
}
string[] lines = textBoxDBOrginal.Text.ToString().Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
List<string> UniqueRecipeItemsInvertedList = new List<string>();
if (lines.Length > 0)
{
//OK it's not null lets look at it.
int lineCount = lines.Length;
string NewCompare = string.Empty;
for (int z = 0; z < lineCount; z++)
{
NewCompare = lines[z];
if (!UniqueRecipeItemsInvertedList.Contains(NewCompare))
{
UniqueRecipeItemsInvertedList.Add(NewCompare);
}
}
}
UniqueRecipeItemsInvertedList.Reverse();
foreach (string s in UniqueRecipeItemsInvertedList)
{
if (!string.IsNullOrEmpty(s))
{
listBox7.Items.Add(s.ToString());
}
}
}
catch (SqlException ex)
{
MessageBox.Show(ex.Errors.ToString());
}
conn.Close();
}
}
答案 0 :(得分:1)
答案已经在这个网站上了。 How to rename duplicates in list using LINQ
代码是:
IEnumerable<String> GetUnique(IEnumerable<String> list)
{
HashSet<String> itms = new HashSet<String>();
foreach(string itm in list)
{
string itr = itm;
while(itms.Contains(itr))
{
itr = itr + "_";
}
itms.Add(itr);
yield return itr;
}
}
答案 1 :(得分:0)
由于Dictionary不允许重复条目(它抛出ArgumentException是未处理的异常),因此处理确保唯一性和序列顺序的“繁重”(仍在测试那个)。我想我正在使用GSI.Sequence错误,因为recipe项目可能有多个序列。 (这不是答案,而是我可以放置代码的地方。我希望我做对了)帽子提示http://williablog.net/williablog/post/2011/08/30/Generic-AddOrUpdate-Extension-for-IDictionary.aspx
while(reader.Read())
{
RecipeItems GSI = new RecipeItems();
GSI.RecipeItem = reader[0].ToString();
GSI.Sequence = Convert.ToInt32(reader[1].ToString());
GSI.RecipeName = reader[2].ToString();
GSI.MaxSequence = Convert.ToInt32(reader[3].ToString());
if (dictionary.ContainsKey(GSI.RecipeItem))
{
dictionary.[GSI.RecipeItem] = GSI.Sequence);
}
else
{
dictionary.Add(GSI.RecipeItem, GSI.Sequence);
}
}
我认为这里的最终答案最终成为我未必预见或渴望的东西。通过大约94个独特的项目,您将在20个食谱的过程中得到大约428个独特配方项目的列表。这将给我一个列表,我可以按正确的顺序获得相应的配方项目。我仍然认为我在这个问题上的逻辑很糟糕,但是当你发现每个食谱中有几个食谱项目乱序并且必须重复而且你将这个时间乘以食谱的数量时,这是有道理的。
答案 2 :(得分:0)
我得出的结论是,尽管可以做到这一点,而且我已经接近了,但我还是没有技能/知识来完成它。
相当于:
我对list&lt;&gt ;, sortedList&lt;&gt;进行了相当多的了解。和列表以及拥有自己的类和方法的能力。例如:RecipeInfo.RecipeItemsList使生活变得如此简单。
我仍然没有弄清楚为什么没有人想要触及这个或为什么它被降级了。在向stackoverflow.com发送另一个问题之前,这种体验可能会让我犹豫不决。