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"}
}
}
2 Answers
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);
}
8 Comments
Sebastian Schumann
That'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.
Alexei Levenkov
@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 - stackoverflow.com/questions/16838136/…
Ulugbek Umirov
@Verarind One of the options may be to display data in the grid.
Ulugbek Umirov
@AlexeiLevenkov Vera rind is not the one who asked the question :)
Alexei Levenkov
@Verarind sorry... You can get list of properties - stackoverflow.com/questions/2634858/… as well as properties by name stackoverflow.com/questions/4939508/…. You can also build Expression tree to dump data...
|
Try this
var result = obj.Select(x => new
{
x.str1,
x.str4
}).ToList();
5 Comments
Sebastian Schumann
That's exacly what my comment says but I think the properties should be dynamically taken from PropertyNames.
Sebastian Schumann
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
Have a look at Ulugbek Umirov's answer. That solves the problem as requested. (Damn he was only seconds faster than me).
Alexei Levenkov
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.
obj.Select(x => new { str1 = x.str1, str4 = x.str4 })but I don't think that this will be easy having dynamic properties.