class OriginalObject
{
public string str1 {get;set;}
public string str2 { get; set; }
public string str3 { get; set; }
public string str4 { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<OriginalObject> obj = new List<OriginalObject>();
obj.Add(new OriginalObject()
{
str1 ="hi",
str2 = "hello",
str3 = "how",
str4 = "r u"
});
obj.Add(new OriginalObject()
{
str1 = "i",
str2 = "am",
str3 = "fine",
str4 = "great"
});
var PropertyNames = new[] { "str1","str4"};
//var result = Select from obj only column names that present in PropertyName Array
// Expected
//obj --->
// {str1 = "hi",str4="r u"}
// {str1 = "i",str4="great"}
}
}
Asked
Active
Viewed 7,275 times
5

Andi AR
- 2,678
- 2
- 23
- 28
-
`obj.Select(x => new { str1 = x.str1, str4 = x.str4 })` but I don't think that this will be easy having dynamic properties. – Sebastian Schumann Aug 28 '15 at 05:28
2 Answers
17
One of the ways how you can do it:
var properties = typeof(OriginalObject).GetProperties()
.Where(p => PropertyNames.Contains(p.Name))
.ToList();
var output = obj.Select(o => {
dynamic x = new ExpandoObject();
var temp = x as IDictionary<string, Object>;
foreach(var property in properties)
temp.Add(property.Name, property.GetValue(o));
return x;
});
Dumping result:
foreach(dynamic x in output)
{
Console.WriteLine(x.str1);
Console.WriteLine(x.str4);
}

Ulugbek Umirov
- 12,719
- 3
- 23
- 31
-
1That's exaclty the solution that's requested but I don't know how to use it dynamically. I was creating the same solution but stopped because there is a runtime array that defines the properties to get. There is no chance to create the 'Dumping result' code because you can't write it at compile time. Knowing this you don't need to use dynamic. A dictionary containing the values will be enough. – Sebastian Schumann Aug 28 '15 at 05:47
-
1@Verarind you may want to ask new question to clarify your goal... It is not clear how you plan to use resulting object - so dynamic sounds perfect answer to this question... If you need real static type - create one at run-time - http://stackoverflow.com/questions/16838136/create-a-lambda-expression-with-a-new-anonymous-type-at-runtime – Alexei Levenkov Aug 28 '15 at 05:50
-
@Verarind One of the options may be to display data in the grid. – Ulugbek Umirov Aug 28 '15 at 05:50
-
@AlexeiLevenkov Vera rind is not the one who asked the question :) – Ulugbek Umirov Aug 28 '15 at 05:51
-
@AlexeiLevenkov It's not my question. I don't plan to use this object. I only read the answer and was a litle bit confused how it's possible to write the 'Dumping Code' when the properties are defined at runtime. – Sebastian Schumann Aug 28 '15 at 05:54
-
@UlugbekUmirov Oh - yeah. Grids... Missed that for the moment. Sorry. – Sebastian Schumann Aug 28 '15 at 05:54
-
2@Verarind sorry... You can get list of properties - http://stackoverflow.com/questions/2634858/how-do-i-reflect-over-the-members-of-dynamic-object as well as properties by name http://stackoverflow.com/questions/4939508/get-value-of-c-sharp-dynamic-property-via-string. You can also build Expression tree to dump data... – Alexei Levenkov Aug 28 '15 at 06:01
-
@AlexeiLevenkov I know dynamic and ExpandoObject. I also answered questions using it but there where other requests. Expression trees and dynamic - wow, never used that before. I'm using a lot of expression tress but without dynamic object. Thanks. Maybe I'll need that in the future. – Sebastian Schumann Aug 28 '15 at 06:11
0
Try this
var result = obj.Select(x => new
{
x.str1,
x.str4
}).ToList();
-
3That's exacly what my comment says but I think the properties should be dynamically taken from PropertyNames. – Sebastian Schumann Aug 28 '15 at 05:31
-
-
Your new anonymous type contains compile time constant properties called `str1` and `str4`. The task was to create an object that takes it's properties from a runtime object called `PropertyNames`. In the specified case there are `str1` and `str4` but in an other case there will be other properties to select. – Sebastian Schumann Aug 28 '15 at 05:39
-
Have a look at Ulugbek Umirov's answer. That solves the problem as requested. (Damn he was only seconds faster than me). – Sebastian Schumann Aug 28 '15 at 05:43
-
I don't see usage of `PropertyNames` in this post... Not really sure how you suggest to construct type at run-time with given properties based on this sample. – Alexei Levenkov Aug 28 '15 at 05:44