Click here to Skip to main content
15,843,145 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can i retrieve a particular value from a generic list after populating the list from database ?

C#
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.
Posted
Updated 22-Nov-12 0:37am
v2
Comments
Rob Branaghan 22-Nov-12 5:53am    
Is it a List<string> or is it List<YourObjectType>?
Abhisheik yk 27-Nov-12 5:54am    
It is a List<Object type>

Use link with foreach to minimize lines:

enlist.Select(i => i.name ==  "sachin").ToList().ForEach(
        i => { 
          //Code here
        });
 
Share this answer
 
v2
C#
int index = anlist.FindIndex(a => a.name == "sachin");  
 if (index > 0)
 {
 // do req operation
 }
 
Share this answer
 
Comments
Master.Man1980 27-Nov-12 14:12pm    
good one!
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
};
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 ?
 
Share this answer
 
Hi Abhisheik

Use the Linq for this, like:
anlist.Where(anlist => anlist.name == "sachin");


Regards
Pawan

Note: Please accpet the solution if it fulfills your critirea.
 
Share this answer
 
Comments
Abhisheik yk 22-Nov-12 6:15am    
Hi Pawan,

I appreciate your response, I am Just trying to explore more methods to do this above operation, Do share if you know any other methods.

Regards,
Abhisheik

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900