开发者

Edit item in list<className> with linq

开发者 https://www.devze.com 2023-02-08 16:41 出处:网络
I have a generic list. It is full. its name is list1. We want to use LINQ, to edit its Items. intput ------> textbox1.text (Key in the list1)

I have a generic list.

It is full.

its name is list1.

We want to use LINQ, to edit its Items.

intput ------> textbox1.text (Key in the list1)

key in class11--------> string _number

What is the code Edit?

Update

Here is what i want:

List lst= new List();

lst.Add("Shekhar1");

lst.Add("Shekhar2");

lst.Add("Shekhar3");

lst.Add("Shekhar4");

lst.Add("Shekhar5");

//code edit item (Shekhar3) -------> Shekhar88 //code to edit the item...?????

Output:

Shekhar1

Shekhar2

Shekhar88

Shekhar4

Shekhar5

for edit (update) the item in generic list:

1- Search with linq to generic list -------> output index item

2-开发者_运维百科 lst.RemoveAt(index)

3- lst.Insert(index, new obj);

ok....!!? please give me code linq. (case 1)

thanx


Well i dont know what you want here but what i think is you want to edit the item in List by using LINQ with some other data may be in a text box.

Anyway Here is an Example to give you an Idea on how o get started: (Run on LINQPAD)

List<string> lst= new List<string>();

lst.Add("Shekhar");
lst.Add("Shekhar");
lst.Add("Shekhar");
lst.Add("Shekhar");
lst.Add("Shekhar");

var edited = (from item in lst  
            select (item = "Pro")).ToList();
lst = edited;

lst.Dump();

Output:

Pro
Pro
Pro
Pro
Pro

Update

Ok based on your suggestions here is the code that will work

string search_string = "Shekhar3";
string replacement_string = "Shekhar88";
lst = (lst.Select( item => {
                          if(item == search_string) return item = replacement_string;
                          else return item;
                              })).ToList();


I think you might mean that you want to edit an item in the list, based on what the user types in? IE: Filter the list of objects based on some predicate?

If this is the case, you could do it with linq like so:

List<MyType> myList = PopulateList();

string key = textBox1.Text;
string newValue = "new value";

// If you only want a single item...
MyType item = (from item in myList
              where item.Key == key
              select key).FirstOrDefault();

// If you want a list of matching items
List<MyType> items = from item in myList
                     where item.Key == key
                     select key;

You can then edit the items you want either directly:

item.Value = newValue;

Or by iterating:

foreach(MyType i in items)
{
    i.Value = newValue;
}

You can achieve the same without LINQ by simply calling the .Where() function with a lambda expression, for example:

List<MyType> items = myList.Where(i => i.Key == key);


You can use Linq to select into a new list, but not modify an existing list. The reason is that Linq deals with enumerables and not lists internally. The same code that works with lists could work with arrays or with a function that returns an enumerable and yields values. Here's how to replace an item using Select() (ideone):

List<string> list = (new string[] { 
    "Shekhar1", "Shekhar2", "Shekhar3", "Shekhar4", "Shekhar5" }
).ToList();

string match = "Shekhar3";
string replace = "Shekhar88";
List<string> newList = list.Select(x => x == match ? replace : x).ToList();
list = newList;
foreach (string value in list) {
    Console.WriteLine("{0}", value);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号