简体   繁体   English

如何使用linq查询分别从列表中获取键和值

[英]How to get both key and value from list separately with linq query

调试屏幕截图 I started off using a randomized array for a quiz that I am doing, but after I got that working I realized that there was no way for me to be able to keep track of the answer that the user selected. 我开始使用随机数组进行测验,但是在完成这项工作后,我意识到我无法追踪用户选择的答案。 I had been shown how to use some shuffle algorithm but it just confused me. 有人向我展示了如何使用一些随机播放算法,但这使我感到困惑。

So I decided to create a List<KeyValuePair> to hold the answers from my dataset, the key is a string that is for the dataset row and the value is an integer, that is 1 for the correct answer then 0 for the wrong answer. 因此,我决定创建一个List<KeyValuePair>来保存我的数据集中的答案,该键是一个用于数据集行的字符串,并且值是一个整数,即正确答案为1,错误答案为0。

So what I am doing is, I have four buttons being stylized with css, I'm turning the button text into one of the four answers. 所以我正在做的是,我有四个用css风格化的按钮,我正在将按钮文本变成四个答案之一。 This may sound like bad programming practice but its the only thing I can come up with and that is to change the tab order attribute to either a 1 or a 0 so I can keep track of the answer to display in a results page at the end. 这听起来像是不好的编程习惯,但我唯一能想到的就是将Tab顺序属性更改为1或0,这样我就可以跟踪答案以显示在结果页的最后。

The code I am using is this... 我正在使用的代码是这样的...

ds = MyDs(1);

System.Random rnd = new System.Random();

var QA = new List<KeyValuePair<string,int>>();

QA.Add(new KeyValuePair<string, int>(ds.Tables[0].Rows[0]["CorrectAnswer"].ToString(),1));
QA.Add(new KeyValuePair<string, int>(ds.Tables[0].Rows[0]["WrongAnswer1"].ToString(), 0));
QA.Add(new KeyValuePair<string, int>(ds.Tables[0].Rows[0]["WrongAnswer2"].ToString(), 0));
QA.Add(new KeyValuePair<string, int>(ds.Tables[0].Rows[0]["WrongAnswer3"].ToString(), 0));

var myQa = QA.OrderBy(c=> rnd.Next()).Select(c=>c.Key.ToString()).ToList();

Label5.Text = ds.Tables[0].Rows[0]["Question"].ToString();

Button1.Text = myQa[0].ToString();
Button2.Text = myQa[1].ToString();
Button3.Text = myQa[2].ToString();
Button4.Text = myQa[3].ToString();

this is fine when you look at myQa, the key shows up like it should but if I do this... 当您查看myQa时这很好,密钥会像应该的那样显示,但是如果我这样做的话...

var myQa = QA.OrderBy(c=> rnd.Next()).Select(c=>c.Key.ToString() + c.Value.ToString()).ToList();

then my buttons text adds the key and value to the text, and I don't want that, I just want the key to show up in the button text, and then be able to access its value. 然后我的按钮文本将键和值添加到文本中,我不希望那样,我只想将键显示在按钮文本中,然后能够访问其值。

Any ideas or where I am missing something? 有什么想法或我想念的地方吗? I'm assuming that its in my query and I'm not sure how to fix it. 我假设它在我的查询中,我不确定如何修复它。

Thanks 谢谢

Fundamentally, you need a Dictionary<TKey, TValue> . 从根本上讲,您需要一个Dictionary<TKey, TValue>

Here: 这里:

var myQa = QA.OrderBy(c=> rnd.Next()).Select(c=>c.Key.ToString() + c.Value.ToString()).ToList();

you are selecting both key and its value . 您同时选择key及其value Instead you can just do: 相反,您可以执行以下操作:

var myQa = QA.OrderBy(c=> rnd.Next()).Select(c=>c.Key.ToString()).ToList(); to get the key 得到钥匙

var kValue = QA.Where(m=> m.Key == "Correct Answer").FirstOrDefault().Value; <- willl get you a value for a particular key. <-willl为您提供特定键的值。

Actually you could use ToDictionary method instead of ToList. 实际上,您可以使用ToDictionary方法代替ToList。 It accepts key selector and value selector parameters. 它接受键选择器和值选择器参数。 And the you can 而你可以

   var myQa = QA.OrderBy(c=> rnd.Next()).ToDictionary(ks=>ks.Key.ToString(), vs=>vs..Value.ToString());
   foreach(var keyValuePair in myQa)
   {
       Button button = new Button();
       button.Text = keyValuePair.Key;
       button.Tag= keyValuePair.Value;
       SomeParentControl.Controls.Add(button);
   }

In this way you'll have both the key and the value in one enumeration. 这样,您将在一个枚举中同时拥有键和值。

Also you can do like this: 您也可以这样:

   var myQaButtons = QA.OrderBy(c=> rnd.Next()).Select(c=>new Button{Text = c.Key, Tag = c.Value}).ToList();
   myQaButtons.ForEach(b=>SomeParentControl.Controls.Add(b));

Or simply 或者简单地

   var myQa = QA.OrderBy(c=> rnd.Next()).Select(c=>new {c.Key, c.Value}).ToList();

   Button1.Text = myQa[0].Key;
   Button1.Tag = myQa[0].Value
   Button2.Text = myQa[1].Key;
   Button2.Tag = myQa[1].Value;
   Button3.Text = myQa[2].Key;
   Button3.Tag = myQa[2].Value;
   Button4.Text = myQa[3].Key;
   Button4.Tag = myQa[3].Value;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM