Click here to Skip to main content
12,507,089 members (63,198 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# ASP.NET C#4.0
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.
Posted 21-Nov-12 23:35pm
Updated 21-Nov-12 23:37pm
v2
Comments
Rob@Love2Code 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>
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Use link with foreach to minimize lines:

enlist.Select(i => i.name ==  "sachin").ToList().ForEach(
        i => { 
          //Code here
        });
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

int index = anlist.FindIndex(a => a.name == "sachin");  
 if (index > 0)
 {
 // do req operation
 }
  Permalink  
Comments
Master.Man1980 27-Nov-12 14:12pm
   
good one!
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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 ?
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160927.1 | Last Updated 22 Nov 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100