Query a list in another list in C# -


i have class that

public class tbl {     public list<row> rows {get; set;} } public class row {     public string name {get; set;}     public value {get; set;} } //using class   //add rows tbl tbl t = new tbl(); t.rows.add(new row() {name = "row1", value = "row1value"}; t.rows.add(new row() {name = "row2", value = "row2value"}; t.rows.add(new row() {name = "row3", value = "row3value"};  //now want select row2 in list, usually, use way public row getrow(this tbl t, string rowname) {     return t.rows.where(x => x.name == rowname).firstordefault(); } row r = t.getrow("row2"); //but use way row r = t.rows["row2"]; 

how can that.

thanks every comments.

extension properties not exist, use wrapper around list<row> , add indexer property it.

public class rowlist : list<row> {      public row this[string key] {         { return this.where( x => x.name == key ).firstordefault(); }     } } 

public class tbl {     public rowlist rows { get; set; } }  tbl t = new tbl(); // ... row r = t.rows["row2"]; 

Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -