Click here to Skip to main content
15,893,190 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I need help to get the columns from list dyanmic using LINQ. Like this,

C#
List<DashBoard> dashboardlist = (List<DashBoard>)objList;

objList = (from obj in dashboardlist select new { obj.EprotexStatus, obj.RecReference, obj.RecDescription, obj.RecDate, obj.ModifiedDate, obj.RectypeDesc }).ToList();

above is working fine to return columns from list using LINQ.

But I want specific columns from list in following process

C#
List<DashBoard> dashboardlist = (List<DashBoard>)objList;
string strColumns = "RecDate,ModifiedDate";
objList = (from obj in dashboardlist select new { strColumns }).ToList();


Above statment i want get two columns only based on string columns variable.

Thanks in advance
Posted
Updated 30-Dec-10 19:11pm
v2
Comments
Hiren solanki 31-Dec-10 4:14am    
generate a wanted list of column directly then pass it to provided LINQ , I think it accepts string so you can.

You can definately do it but it needs some extra efforts.

Use Dynamic Linq Library[^]

Thanks.
 
Share this answer
 
Comments
P.T.R.K 31-Dec-10 4:12am    
Thank you your reply.

For this link(Dynamic Linq Library), there is specified columns are returned. like p.productname,p.productid.

I need columns will come different condition. In my exampe, if dashboard then get RecDate,ModifiedDate and if order then get OrderDate,ModifiedDate.
Is it possible?
Hi All,

I got solution for using different method. Like this,

I converted the list to DataSet and then following method
DataView view = new DataView();
DataTable dtTable=view.ToTable(false,<string array="">);
</string>


It is working fine my requirement

Thank you for all.
 
Share this answer
 
You need to use the "dynamic" feature available in C# 4.0, because the list of properties is not known at compile time. A small helper method called 'Projection' (see below) could help to get individual properties from source objects.


Here is a quick example:
C#
public void Test() {
    var data = new[] {
        new TestData { X = 1, Y = 2, Z = 3 }
    ,   new TestData { X = 2, Y = 4, Z = 6 }
    };
    var strColumns = "X,Z".Split(',');
    foreach (var item in data.Select(a => Projection(a, strColumns))) {
        Console.WriteLine("{0} {1}", item.X, item.Z);
    }
}
private static dynamic Projection(object a, IEnumerable<string> props) {
    if (a == null) {
        return null;
    }
    IDictionary<string,object> res = new ExpandoObject();
    var type = a.GetType();
    foreach (var pair in props.Select(n => new {
        Name = n
    ,   Property = type.GetProperty(n)})) {
        res[pair.Name] = pair.Property.GetValue(a, new object[0]);
    }
    return res;
}
class TestData {
    public int X { get; set; }
    public int Y { get; set; }
    public int Z { get; set; }
}
 
Share this answer
 
Comments
P.T.R.K 12-Jan-11 22:52pm    
Thank you your post.
I will be try to implement this way.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900