0
//list is IEnumeable NOT IEnumerable<T>

var IEnumerable<object> = list.AsQueryable().Cast<object>().Select(x=> .........);

object actually has a POCO underlying Anonymous class e.g

AccountId,Name,SecretInfo

What I want in the select statement is

AccountId = x.GetType().GetProperty("AccountId").GetValue(x,null),
Name = x.GetType().GetProperty("Name").GetValue(x,null)

Also I want to hide the SecretInfo Column which I can pass as a hardcoded string "SecretInfo" Basically the select list needs to be built up dynamically on the Anonymous type.... How can this be done....Any Linq punters out there who can help me?

chugh97
  • 9,602
  • 25
  • 89
  • 136
  • Are you aware that an anonymous class is still strongly-typed? And why is list IEnumerable and not IEnumerable? – TGlatzer Jun 26 '13 at 11:53
  • I'm using IEnumerable because its generic and I can't change it – chugh97 Jun 26 '13 at 12:31
  • What's your datasource, which makes that impossible? I barely can imagine cases where strong typing is not possible (besides Interop) – TGlatzer Jun 26 '13 at 12:42
  • It is a generic framework hence I can't do anything about it. I just need the select list maybe using reflection ?? – chugh97 Jun 26 '13 at 12:45
  • Okay - there will be some class, which has the data in the your generic framework (by the way it's very helpfull for the helpers to get some not so abstract information - but that's for the future only), which you probably could use - you could look into the type with an debugger and then cast to the correct type. Nevertheless you can also cast to dynamic: `list.Select(x => ((dynamic)x).AccountId)` But this is really not recommended. – TGlatzer Jun 26 '13 at 13:24

1 Answers1

0

The answer to your question relies on anonymous types. The following code is what you can use:

var result = list.AsQueryable().Cast<Info>().Select(x => new
    {
        AccountId = x.AccountId,
        Name = x.Name
    });

Between the brackets that follow the new keyword in the select statement, you are creating an anonymous type that will have two implicitly typed read-only fields (AccountId and Name). Hope this helps!

I would like to post this quote from the linked (no pun intended) article:

Anonymous types typically are used in the select clause of a query expression to return a subset of the properties from each object in the source sequence. For more information about queries, see LINQ Query Expressions (C# Programming Guide).

Will Custode
  • 4,576
  • 3
  • 26
  • 51