score:6
when your getting the item from a list its an reference type, if your updating anything to it then it will automatically change the values in list. please check your self after updating...........
item whichever your getting from
userdata.where(s => s.id == idkey).tolist();
is an reference type.
score:0
if i'm misunderstanding you then please correct me, but i think you're saying that you essentially want to iterate through the elements of a list, and if it matches a condition then you want to alter it in some way and add it to another list.
if that's the case, then please see the code below to see how to write an anonymous method using the where clause. the where clause just wants an anonymous function or delegate which matches the following:
parameters: elementtype element, int index -- return: bool result
which allows it to either select or ignore the element based upon the boolean return. this allows us to submit a simple boolean expression, or a more complex function which has additional steps, as follows:
using system;
using system.collections.generic;
using system.linq;
using system.text;
namespace stackoverflow
{
class program
{
static void main(string[] args)
{
int idkey = 1;
list<someclass> userdata = new list<someclass>()
{
new someclass(),
new someclass(1),
new someclass(2)
};
//this operation actually works by evaluating the condition provided and adding the
//object s if the bool returned is true, but we can do other things too
userdata.where(s => s.id == idkey).tolist();
//we can actually write an entire method inside the where clause, like so:
list<someclass> filteredlist = userdata.where((s) => //create a parameter for the func<someclass,bool> by using (varname)
{
bool thebooleanthatactuallygetsreturnedintheaboveversion =
(s.id == idkey);
if (thebooleanthatactuallygetsreturnedintheaboveversion) s.name = "changed";
return thebooleanthatactuallygetsreturnedintheaboveversion;
}
).tolist();
foreach (someclass item in filteredlist)
{
console.writeline(item.name);
}
}
}
class someclass
{
public int id { get; set; }
public string name { get; set; }
public someclass(int id = 0, string name = "defaultname")
{
this.id = id;
this.name = name;
}
}
}
score:1
just fetch the object using singleordefault
and make related changes; you do not need to add it to the list again; you are simply changing the same instance which is an element of the list.
var temp = userdata.singleordefault(s => s.id == idkey);
// apply changes
temp.x = somevalue;
score:2
as long as userdata
is reference type, the list only holds references to instances of that object. so you can change its properties without the need of remove/insert (and obviously do not need index of that object).
i also suggest you want to use single
method (instead of tolist()
) as long as the id is unique.
example
public void changeusername(list<userdata> users, int userid, string newname)
{
var user = users.single(x=> x.userid == userid);
user.name = newname; // here you are changing the name value of userdata objects, which is still part of the list
}
score:7
you can get the index using the method
userdata.findindex(s => s.id == idkey)
it will return an int.
Source: stackoverflow.com
Related Query
- Insert an object in the list in C#
- Linq code to get the index of an object in an array from an object within a list
- Why is my code returning a list of the same object 4 times?
- I am wondering about the state of connection and impact on code performance by 'yield' while iterating over data reader object
- Generating the Shortest Regex Dynamically from a source List of Strings
- LINQ Where clause with Contains where the list has complex object
- Using Linq, how to GroupBy against objects in a list within the target object
- Operation is not valid due to the current state of the object - Linq on List
- How to find how many times the same object appears in a list and then find a property of the most appeared object
- How to remove duplicates from a list of custom objects, by a property of the object
- Using LINQ to get the difference between two list of objects based only on a single object property
- Linq to return a new object with a selection from the list
- How can I speed up this linq query on a List<> with search criteria on 3 attributes in the list object
- Group objects in List<T> by id and order the List by duplicateCount for each object
- Find the List index of the object containing the closest property value
- What is the best way to get the percentage for an object in linq list and map it to JSON?
- Using Linq to merge all List returned by the method of an object in a List
- How to create a list of one object attributes from the actual list of objects?
- Elegant way to check if a list contains an object where one property is the same, and replace only if the date of another property is later
- List of string into List of object by splitting the string by delimiter
- Add range to a new list of object within the same command / line
- How can I check the type of an object against a list of types?
- Fastest way to insert a value in int List and then sort the list
- Given a list of color objects, find the most/least frequently existing color, and return that as a color object
- What is the correct way of retrieving a given business object from a list of business objects using LINQ Where vs. Find?
- How do I filter a list within a list while maintaining the original form of the object using linq?
- Need to return an object with the lowest property value from List of objects
- Selecting inner object of the list
- How to fetch a list from the database based on another list of a different object type?
- Get the next object in list after the object with current ID
More Query from same tag
- How to Translate this LINQ Query to use Lambda Expressions Instead?
- Get Duplicates in Range of Numbers
- Joining groups in a List using LINQ
- LINQ to DataTable, finding duplicate rows
- LINQ query over a list of Bool+String pairs. How to join strings without foreach?
- Does the compiler concatenate LINQ where queries?
- How to perform Linq`s group by and sum operation on List<Dictionary<string, object>>
- Linq query returns corresponding sql when executed
- Entity Framework exclude result
- LINQ Distinct rows with multiple joins
- List<object> select use multiple values
- SubSonic Outer Join
- Linq to get common values while grouping DataTable
- Bind Objects To DataGridview and Reorder Column
- LINQ Duplicate (un)tagging method
- Ugly LINQ statement, a better way?
- Cast to double failing?
- Is it possible to fill a HashTable with multiple keys from a Linq query?
- Join into with column and variable value compared using Linq or Entity Framework queries
- how to convert the stored procedure to Linq
- Linq statement return error when no record found
- Flatten and group complex objects in Linq and preserve null children
- Dynamic lambda expression (OrderBy) and nullable property type
- When using Query Syntax in C# "Enumeration yielded no results". How to retrieve output
- Sorting a list with null values
- Get nodes attributes in xdocument
- Querying against LINQ to SQL relationships
- How can I avoid repeating some part of the statement in this LINQ
- How to OrderBy by related entity in EntityFramework
- Entity Framework LINQ - dynamically select properties