My application uses a list like this:
List<MyClass> list = new List<MyClass>();
Using the Add
method, another instance of MyClass
is added to the list.
MyClass
provides, among others, the following methods:
public void SetId(String Id);
public String GetId();
How can I find a specific instance of MyClass
by means of using the GetId
method? I know there is the Find
method, but I don't know if this would work here?!
This question is related to
c#
list
properties
find
var list = new List<MyClass>();
var item = list.Find( x => x.GetId() == "TARGET_ID" );
or if there is only one and you want to enforce that something like SingleOrDefault
may be what you want
var item = list.SingleOrDefault( x => x.GetId() == "TARGET" );
if ( item == null )
throw new Exception();
Try:
list.Find(item => item.id==myid);
You can also use LINQ extensions:
string id = "hello";
MyClass result = list.Where(m => m.GetId() == id).First();
You can create a search variable to hold your searching criteria. Here is an example using database.
var query = from o in this.mJDBDataset.Products
where o.ProductStatus == textBox1.Text || o.Karrot == textBox1.Text
|| o.ProductDetails == textBox1.Text || o.DepositDate == textBox1.Text
|| o.SellDate == textBox1.Text
select o;
dataGridView1.DataSource = query.ToList();
//Search and Calculate
search = textBox1.Text;
cnn.Open();
string query1 = string.Format("select * from Products where ProductStatus='"
+ search +"'");
SqlDataAdapter da = new SqlDataAdapter(query1, cnn);
DataSet ds = new DataSet();
da.Fill(ds, "Products");
SqlDataReader reader;
reader = new SqlCommand(query1, cnn).ExecuteReader();
List<double> DuePayment = new List<double>();
if (reader.HasRows)
{
while (reader.Read())
{
foreach (DataRow row in ds.Tables["Products"].Rows)
{
DuePaymentstring.Add(row["DuePayment"].ToString());
DuePayment = DuePaymentstring.Select(x => double.Parse(x)).ToList();
}
}
tdp = 0;
tdp = DuePayment.Sum();
DuePaymentstring.Remove(Convert.ToString(DuePaymentstring.Count));
DuePayment.Clear();
}
cnn.Close();
label3.Text = Convert.ToString(tdp + " Due Payment Count: " +
DuePayment.Count + " Due Payment string Count: " + DuePaymentstring.Count);
tdp = 0;
//DuePaymentstring.RemoveRange(0,DuePaymentstring.Count);
//DuePayment.RemoveRange(0, DuePayment.Count);
//Search and Calculate
Here "var query" is generating the search criteria you are giving through the search variable. Then "DuePaymentstring.Select" is selecting the data matching your given criteria. Feel free to ask if you have problem understanding.
public List<DealsCategory> DealCategory { get; set; }
int categoryid = Convert.ToInt16(dealsModel.DealCategory.Select(x => x.Id));
Or if you do not prefer to use LINQ you can do it the old-school way:
List<MyClass> list = new List<MyClass>();
foreach (MyClass element in list)
{
if (element.GetId() == "heres_where_you_put_what_you_are_looking_for")
{
break; // If you only want to find the first instance a break here would be best for your application
}
}
You can solve your problem most concisely with a predicate written using anonymous method syntax:
MyClass found = list.Find(item => item.GetID() == ID);
Source: Stackoverflow.com