将项添加到列表c# [英] Adding Items to A List c#

查看:61
本文介绍了将项添加到列表c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个二维列表和一个按钮,我在其中添加项目到列表并尝试获取项目并将它们添加到列表框中。这是问题,当将列表的项目添加到列表框时,我想根据哪个List< string>添加不同的字符串/文本。它是基于某种条件的。我尝试过使用LINQ。例如:

列表< List< string>> getallwords; 
Main mainlib; // 类对象

private void btnAddToList_Click( object sender,EventArgs e)
{
listbox1.Items.Clear();
if (textboxCount.Text == 3
{
mainlib.AddtoList(tbox1.Text,tbox2.Text,tbox3.Text); // 类中添加三个单词的方法
getallwords = mainlib.GetWordList() // 获取列表中项目的方法
var 3wordsquery = getallwords.Where(item = > item.Count == 3

foreach (List< string> item in 3wordsquery)
{
listbox1.Items.Add( 房屋数量为3);
}
}

if (textboxCount.Text == 2
{
mainlib.AddtoList(tbox1.Text,tbox2.Text); // 只添加两个单词列表的方法
getallwords = mainlib.GetWordList()< span class =code-comment> // 获取列表中项目的方法
var 2wordsquery = getallwords.Where(item = > item.Count == 2
foreach (List< string> item in 2wordsquery)
{
listbox1.Items.Add( 房子数量为2);
}
}
}



但是代码显示,每当项目被添加到列表框中时,它都会返回项目计数为2或项目计数为3.如何将所有项目返回但仍然将不同的文本添加到列表框?

解决方案

我想你想要获取每个列表元素的房屋数量。您可以通过这种方式实现:



列表< List< string>> getallwords =  new 列表< List< string>>(){
new 列表< string> ; { < span class =code-string> B, C D},
new 列表< string> { E F G },
new 列表< string> { H },
new List< string> { J}};

var result = getallwords
.GroupBy(item => item.Count())
.Select( grp = > new
{
Message = string .Format( 组中房屋的数量:{0},grp.Key)
});





结果:

一组房屋数量:4 
一组房屋数量:3
一组房屋数量:2
房屋数量一个组:1


我将假设这里的关键问题是将文本内容传递给'mainlib.AddtoList方法的TextBox假设有一个这样的输入参数:



mainlib.AddtoList(params string [] args)



假设你有一个使用的文本框数组f或输入名为'TbxAry:

  private  TextBoxEx [] TbxAry; Form_Load或其他代码中

//
TbxAry = new TextBoxEx []
{
textBox1,textBox2,textBox3,textBox4 // 依此类推......
};

// 需要System.Linq
private void SomeButton_Click( object sender,EventArgs e)
{
mainlib.AddtoList(TbxAry
.Where(tbx = > (!string.IsNullOrEmpty(tbx.Text)))
。选择(tbx = > tbx.Text)。ToArray()
);
}

我会删除一个单独的TextBox来保存'Count,并以其他方式处理。你可以考虑使用这样的字典:



字典< int,List< List< string>>> NToStrings;



在你调用mainlib.AddToList之前会更新...



这样,你就永远不会使用Linq并取出具有相同数量项目的列表。你只需这样做:

 List< List< string>>结果; 

if (NToStrings.TryParse( 3 out results))
{
// 你有一个结果中有3个元素的所有列表的列表...执行任何操作
{
else
{
// 没有结果......做任何事
}


I have a "2 dimensional" list, and a button in which I add items to the list and try to get the items and add them to a listbox. This is the problem, when adding the items of the list to the listbox, I want to add different strings/texts depending on which List<string> it is, based on a certain condition. I've tried using LINQ. So For example:

List<List<string>> getallwords;
Main mainlib; //class object

private void btnAddToList_Click(object sender, EventArgs e)
{
   listbox1.Items.Clear();
   if(textboxCount.Text == "3")
   {
      mainlib.AddtoList(tbox1.Text, tbox2.Text, tbox3.Text); //method from class that adds three words to list
      getallwords = mainlib.GetWordList() //method to get items in list
      var 3wordsquery = getallwords.Where(item => item.Count == 3)

      foreach(List<string> item in 3wordsquery)
      {
         listbox1.Items.Add("The houses are 3 in number");
      }
   }

   if(textboxCount.Text == "2")
   {
      mainlib.AddtoList(tbox1.Text, tbox2.Text); //method to add just two words to list
      getallwords = mainlib.GetWordList() //method to get items in list
      var 2wordsquery = getallwords.Where(item => item.Count == 2)
      foreach(List<string> item in 2wordsquery)
      {
         listbox1.Items.Add("The house are 2 in number");
      }
   }
}


But as the code shows, whenever the items are being added to the listbox, it returns either where item count is 2 or item count is 3. How can I make all items to be returned but still add different texts to the listbox?

解决方案

I guess you want to get the count of houses in for each list element. You're able to achieve that this way:

List<List<string>> getallwords = new List<List<string>>(){
    new List<string>{"A", "B", "C", "D"},
    new List<string>{"E", "F", "G"},
    new List<string>{"H", "I"},
    new List<string>{"J"}};

var result = getallwords
    .GroupBy(item=>item.Count())
    .Select(grp=> new
    {
        Message = string.Format("The count of houses in a group: {0}", grp.Key)
    });



Result:

The count of houses in a group: 4
The count of houses in a group: 3
The count of houses in a group: 2
The count of houses in a group: 1


I'm going to assume the key issue here is getting the TextBoxes that have text content passed to the 'mainlib.AddtoList method which I assume has an input parameter like this:

mainlib.AddtoList(params string[] args)

Assuming you have an Array of the TextBoxes used for input named 'TbxAry:

private TextBoxEx[] TbxAry;

// in Form_Load or other code:
TbxAry = new TextBoxEx[]
{
    textBox1, textBox2, textBox3, textBox4 // and so on ...
};

// requires System.Linq
private void SomeButton_Click(object sender, EventArgs e)
{
    mainlib.AddtoList(TbxAry
        .Where(tbx => (!string.IsNullOrEmpty(tbx.Text)))
        .Select(tbx => tbx.Text).ToArray()
    );
}

I would eliminate a separate TextBox to hold a 'Count, and handle that some other way. You might consider using a Dictionary like this:

Dictionary<int, List<List<string>>> NToStrings;

which would be updated before you call mainlib.AddToList...

That way, you'd never have use Linq and pull out the Lists that have the same number of items. You'd just do this:

List<List<string>> results;
    
if(NToStrings.TryParse(3, out results)) 
{
   // you have a list of all lists with 3 elements in 'results ... do whatever
{
else
{
   // no results ... do whatever
}


这篇关于将项添加到列表c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆