C#: How to convert a list of objects to a list of a single property of that object?
List<string> firstNames = people.Select(person => person.FirstName).ToList();
And with sorting
List<string> orderedNames = people.Select(person => person.FirstName).OrderBy(name => name).ToList();
Get list of properties from List of objects
LINQ is the answer. You can use it to "project" from your object collection to another collection - in this case a collection of object property values.
List<string> properties = objectList.Select(o => o.StringProperty).ToList();
How to collect a single property in a list of objects?
You could do:
public static List<TProp> GetProperties<T, TProp>(this IEnumerable<T> seq, Func<T, TProp> selector)
{
return seq.Select(selector).ToList();
}
and use it like:
List<int> speeds = motions.GetProperties(m => m.Speed);
it's questionable whether this method is better than just using Select
and ToList
directly though.
Convert a list of objects to an array of one of the object's properties
You are looking for
MyList.Select(x=>x.Name).ToArray();
Since Select
is an Extension method make sure to add that namespace by adding a
using System.Linq
to your file - then it will show up with Intellisense.
C# extract property from list of objects
ListOfC.Select(c => c.name).ToList();
Select
is a transform function, it returns a new IEnumerable
based on the selector it is given. As you specifically want a List
, you need to cast it to a List
afterwards.
Return a single object from a list of C# objects checking for matching properties
Your input is an enumeration of some specific kind of objects and you have to create an object of the same type where all properties are filled, where all values are the same. This could be done with something like this:
private static T GetCommonProperties<T>(IEnumerable<T> source) where T : new()
{
var first = true;
var common = new T();
var props = typeof(T).GetProperties();
foreach (var item in source)
{
if (first)
{
first = false;
foreach (var prop in props)
{
var value = prop.GetValue(item, null);
prop.SetValue(common, value);
}
}
else
{
foreach (var prop in props)
{
var itemValue = prop.GetValue(item, null);
var commonValue = prop.GetValue(common, null);
if ((dynamic)itemValue != (dynamic)commonValue)
{
prop.SetValue(common, GetDefault(prop.PropertyType));
}
}
}
}
return common;
}
The given method is not really optimal, cause it uses the dynamic
trick to solve the comparison problem of boxed values. Also getting the default value for a specific type could probably implemented by this generic approach:
private static object GetDefault(Type t)
{
return typeof(Program)
.GetMethod(nameof(GetDefaultValue), BindingFlags.NonPublic | BindingFlags.Static)
.MakeGenericMethod(t)
.Invoke(null, null);
}
private static T GetDefaultValue<T>()
{
return default;
}
But it would also be possible to provide a switch statement or Dictionary<Type, object>
that returns the desired default value if no match available.
Last but not least, another possible performance improvement would be to remove all PropertyInfo
entries from the props
variable, cause for any next upcoming object, this check is not necessary anymore and the same way, the loop could be early exited, when no more props are available anymore.
A working example can be found here.
Can I get a single properties values from a Listobject?
This is the purpose of the Select
extension method:
properties.AddRange(ListObject.Select(o => o.PropA));
The Select
will project the selected property of each object into a new enumerable.
Create a single item Liststring from a Listobject
Just use LINQ:
List<string> names = groups.Select(x => x.name).ToList();
This will Select() all the names, and turn it into a List<string>
via ToList()
Related Topics
How Are Nullable Types Implemented Under the Hood in .Net
How to Programmatically Log in to a Website to Screenscape
Xaml Gridview Itemtemplate Not Binding to Control
JSON.Net Adding Backslash While Returning JSON Serialized String
Using Ssl and Sslstream for Peer to Peer Authentication
Including Pictures in an Outlook Email
Get the Object Is Null Using JSON in Wcf Service
Unit Testing That Events Are Raised in C# (In Order)
JSON Serializer Object with Internal Properties
Datetime.Adddays() Not Working as Expected
How to Access Elements of a Jarray (Or Iterate Over Them)
Dynamic Button Creation & Placing Them in a Predefined Order Using C#
Read Big Txt File, Out of Memory Exception
How to Run Code Before Program Exit