【发布时间】:2013-08-06 21:42:48
【问题描述】:
我真的被困住了,在这里发布解决方案的你们中的许多人总体上都很聪明(恕我直言),我想我会看看你们中的佼佼者能解决这个问题。
一些背景
我正在尝试创建一个列表,该列表必须仅包含特定序列中的不同项目。 (它是一个主键,因此必须是不同的(我没有把它作为主键,但我必须使用我给定的东西,你知道它是怎么回事)。
为便于理解此要求,请考虑从食谱书中创建不同的食谱步骤列表。我的问题是这些“食谱”的“厨师”经常改变他们创作杰作的顺序。
例如:
食谱 1
- 用叉子搅打鸡蛋
- 在平底锅中融化人造黄油
- 倒入鸡蛋
- 不断搅拌
- 盘子
- 根据需要添加盐和胡椒
食谱 2
- 鸡蛋打入碗中
- 用叉子搅打鸡蛋
- 在平底锅中用小火融化人造黄油
- 倒入鸡蛋
- 不断搅拌
- 盘子
- 服务
- 根据需要添加盐和胡椒
食谱 3
- 用叉子搅打鸡蛋
- 根据需要添加盐和胡椒
- 在平底锅中用小火融化人造黄油
- 倒入鸡蛋
- 不断搅拌
- 盘子
如您所见,“添加盐和胡椒...”不能在配方 3 中排在第 2 位,但在配方 1 和 2 中仍保持正确的顺序。
我认为,如果我可以识别“违规”列表项并在其末尾添加一个句点,从而使其独一无二,这将是一个解决方案。
如果给定一个数据集(由 SQL 查询获取),并且以正确的顺序重复并放入字符串类型的列表中,我该如何在 C# 中执行此操作? LINQ 在这里不是必需的,但如果它提供了解决方案,我不害怕使用它。
具体代码(或伪代码):
- 标识需要复制和修改的列表项。
- 确定新创建的大列表中的 WHERE(假设)是要放置的新修改的列表项。
如果您的第一个问题是“向我展示您的工作”,请注意我已经为此做了很多工作,而且代码通常很长。
我很高兴使用伪代码或使用我的数据集尝试您的代码。
我也很高兴阅读其他可能相关的解决方案。
谢谢,我期待看到您的解决方案。
--edit:我开始觉得如果你不发布代码,人们会不喜欢它。 所以就到这里了(我在上面说它很长)。该代码有效,但不能解决问题。它按顺序返回一个不同的列表,没有重复。 (如果以下格式不好请见谅)
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();
}
}
【问题讨论】:
-
为什么降级?如果你要降级它,你至少应该说出原因,以便我下次知道得更好。
-
猜猜这从雷达上消失了。令人失望。
-
我发现这个 [link] stackoverflow.com/questions/8449999/… DavidB [link] stackoverflow.com/users/8155/david-b 在他的 C# 示例中做得很好(恕我直言)。如果我能全神贯注地将这个解决方案应用于我的问题,这就很接近了。 :)
-
好的,看起来 C# 关键字 Dictionary 可能是这里的关键(请原谅双关语)dotnetperls.com/dictionary。
标签: c# sql list comparison string-comparison