从通用列表中检索特定值 [英] Retrieve a particular value from Generic List

查看:49
本文介绍了从通用列表中检索特定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从数据库填充列表后,如何从通用列表中检索特定值?

How can i retrieve a particular value from a generic list after populating the list from database ?

for(int i = 0; i < anlist.Count; i++)
        {
        var text = anlist[i];
            if (text.name == "sachin")
            {
                // do req operation
            }
        }



我已经使用了它,并且工作正常,但是我想知道是否还有其他方法可以做到这一点.



I have used this and its working fine, But i want to know if there are any other Methods to do the same.

推荐答案

使用带有foreach的链接以最小化行:

Use link with foreach to minimize lines:

enlist.Select(i => i.name ==  "sachin").ToList().ForEach(
        i => { 
          //Code here
        });


int index = anlist.FindIndex(a => a.name == "sachin");  
 if (index > 0)
 {
 // do req operation
 }


看看您对anList所做的操作,我们可以推断出anList中的每个元素不是 一个简单的Type,而是一个Object of某种类型,因为您正在访问anList中每个项目的属性:".name" ...

为了避免在anList上进行for循环或forall迭代,您将不得不使用其他数据结构.

您可以考虑许多数据结构,但是我会根据您最可能会最频繁地使用anList中包含的对象及其内部结构来进行选择.

内部结构:例如,您的对象的多个实例的.name属性值是否相同:或者每个.name属性值是否唯一?

如果所有.name属性值都是 unique 字符串:您可以使用Dictionary [string,anList],其中该字符串与anList中每个对象的.name属性相对应.

但是,如果您的许多对象都包含相同的.name属性,并且您需要对所有匹配项执行操作或提取所有匹配项的集合:那就是另一回事了.

考虑一下WinForms项目中的一小段代码,其中四个Form放在一个Form上,并且它们都有其默认的唯一名称:
Looking at what you are doing with anList, we can infer that each element in anList is not a simple Type, but an Object of some type, because you are accessing a Property of each item in anList: ".name" ...

To avoid for-loop, or forall iteration, over anList, you are going to have to use a different data structure.

There are many data structures you could consider, but I would be making my choice based on what you know you are most probably going to be doing most frequently with whatever your objects contained in anList are, and their internal structure.

Internal structure: for example, could there be multiple instances of your objects whose .name Property value is the same: or, is each .name Property value unique ?

If all .name Property values are unique strings: you could use a Dictionary[string, anList], where the string corresponds to what is now the .name property of each object in anList.

However, if many of your objects contain a .name Property that is identical, and you need to perform an operation on, or pull-out a collection of, all matches: that''s another story.

Consider this small fragment of code from a WinForms project, where four CheckBoxes are put on a Form, and they all have their default unique names:
Dictionary<string, CheckBox> cbDict = new Dictionary<string, CheckBox>(
);

cbDict.Add(checkBox1.Name, checkBox1);
cbDict.Add(checkBox2.Name, checkBox2);
cbDict.Add(checkBox3.Name, checkBox3);
cbDict.Add(checkBox4.Name, checkBox4);

CheckBox nameMatchCheckBox;

// if you were absolutely certain that
// each key was present, you could omit
// this test for the presence of the key
// and the test for no-match below
if (cbDict.Keys.Contains("checkBox3"))
{
    nameMatchCheckBox = cbDict["checkBox3"];
}

// at this point ''nameMatchCheckBox is either ''null
// or contains a reference to a CheckBox object
// so you can then test for non-null, and do whatever with
// the CheckBox object:
//
if (nameMatchCheckBox != null) 
{ 
    // whatever
};

注意:我在这里省略了对Linq的可能使用,这也许可以导致更简单的解决方案.这也是创建扩展的好机会吗?

Note: I''ve omitted possible use of Linq here, which, perhaps, could lead to a simpler solution. This also might be a good opportunity to create an extension ?


这篇关于从通用列表中检索特定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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