3

If I have a list of objects, eg

List<CustomClass> dataList = new List<CustomClass>
{
    new CustomClass { Field1 = "a1", Field2 = "a2", Field3 = "a3", Field4 = "a4", Field5 = "a5"},
    new CustomClass { Field1 = "b1", Field2 = "b2", Field3 = "b3", Field4 = "b4", Field5 = "b5"},
    new CustomClass { Field1 = "c1", Field2 = "c2", Field3 = "c3", Field4 = "c4", Field5 = "c5"},
    new CustomClass { Field1 = "d1", Field2 = "d2", Field3 = "d3", Field4 = "d4", Field5 = "d5"},
};

public class CustomClass
{
    public string Field1 { get; set; }
    public string Field2 { get; set; }
    public string Field3 { get; set; }
    public string Field4 { get; set; }
    public string Field5 { get; set; }
}

and a list of selected property names, eg

List<string> propertiesSelectedList = new List<string> { "Field1", "Field3", "Field5" };

How can I dynamically Select only the required properties at runtime using LINQ?

Eg equivalent of the following but at runtime using the values in list propertiesSelectedList?

var newListWithSelectedProperties = dataList.Select(n => new { n.Field1, n.Field3, n.Field5 }).ToList();

The final output would hopefully would look like this list...

List<T> newdataList = new List<T>
{
    new T { Field1 = "a1", Field3 = "a3", Field5 = "a5"},
    new T { Field1 = "b1", Field3 = "b3", Field5 = "b5"},
    new T { Field1 = "c1", Field3 = "c3", Field5 = "c5"},
    new T { Field1 = "d1", Field3 = "d3", Field5 = "d5"},
};
  • 4
    possible duplicate (https://stackoverflow.com/questions/32263862/c-sharp-create-object-with-dynamic-properties-linq-select-listobject-values). This uses a combination of `Linq` and `ExpandoObject`, which I think would suit your needs. – Ryan Wilson Oct 24 '19 at 14:50
  • 1
    do you intend to have anonymous objects as the resulting collection? is this on purpose or did you write it because you don't know how else to demonstrate what you want? – Mong Zhu Oct 24 '19 at 14:52
  • @MongZhu I don't know how to demonstrate what I want. I just want to limit the new list to fields contained in a List. –  Oct 24 '19 at 14:54
  • do you want the result to be a flat list? or do you still want that the values `"Field1", "Field3", "Field5"` of each object are preserved as a group? – Mong Zhu Oct 24 '19 at 14:58
  • I've updated my question to show desired output object. –  Oct 24 '19 at 15:01
  • You have said "Required" properties. How do you want to determine the required properties? Are you going the decorate the properties with RequiredAttribute? so do you want the out a list of object according to your example output or list of strings ? – codein Oct 24 '19 at 15:15
  • I don't mean RequiredAttribute, I just mean the 'required' properties (or desired properties) will be listed in `List propertiesSelectedList = new List { "Field1", "Field3", "Field5" };` –  Oct 24 '19 at 15:17
  • ok, then it seems that you have to work with a dynamic type like in the duplicate. – Mong Zhu Oct 24 '19 at 15:20
  • @codein - I think you've misunderstood. I don't want this in the where filtering clause but in Select to specify returned properties. It looks like the duplicate links provided will get me to where I can do this. Thanks everyone. –  Oct 24 '19 at 15:29
  • yes. just realized. that's I removed the comment. thanks – codein Oct 24 '19 at 15:30

0 Answers0