Dynamic property access for generated LINQ classes

How can I access dynamic properties from the generated LINQ class?

Reason: I would like to be able to customize the displayed columns of the table where Partner is the LINQ class created in the SQL Server database table.

<table class="grid">
  <thead>
   <tr>
     <% foreach (Column c in (IEnumerable)ViewData["columns"]) { %>
     <th><%= c.Title %></th>    
     <% } %>                               
   </tr>
 </thead>
 <tbody>
 <% foreach (Partner p in (IEnumerable)ViewData.Model) { %>
   <tr>
     <% foreach (Column c in (IEnumerable)ViewData["columns"]) { %>
?????   <th> <%= p.GetProperty(c.Name) %>  </th>  ?????
     <% } %>         
   </tr>       
 <% } %>
  </tbody>
</table>

Any idea what the method code might look like p.GetProperty(c.Name)?

Forgive me if the question is very simple, but since I am new to C # and LINQ, I really could not understand.

+3
source share
4 answers

The reflection here is pretty good, but in fact - everything is known at compile time. Thus, you can specify everything during development.

public class DataItem
{
  string Title {get;set;}
  object Value {get;set;}
}

public interface IDataItems
{
  IEnumerable<DataItem> Items()
}


//suppose LINQ gives you this:
public partial class Customer
{
  public string Name {get;set;}
  public string Address {get;set;}
  public int Age {get;set;}
}

//then you add this in another file.
//if there a lot of this, it can be code genned by design-time reflection
public partial class Customer : IDataItems
{
  public IEnumerable<DataItem> IDataItems.Items()
  {
    yield return new DataItem() {"Name", Name};
    yield return new DataItem() {"Address", Address};
    yield return new DataItem() {"Age", Age};
  }
}

//and the foreach loops look like this:
foreach(DataItem d in ViewData.OfType<IDataItems>().First().Items())
{
  d.Title;
}

foreach(IDataItems container in ViewData.OfType<IDataItems>())
{
    foreach(DataItem d in container.Items())
    {
       d.Value;
    }
}
+1
source

, - ,

typeof(Partner).GetProperty(c.Name).GetValue(p, null)

:

var columns = (IEnumerable<string>)ViewData["columns"];
var cols = columns.Select(colName =>
      typeof(Partner).GetProperty(colName)).ToList();

PropertyInfo , :

 foreach (var col in cols) { %>
     <th><%= col.GetValue(p,null) %></th>
 <% }

( <th/> <td/>, ?)

, . ().

+4

, , :

p.GetType().GetProperty(c.Name).GetValue(p)
+2

. . !

. Linq! .

http://kirillosenkov.blogspot.com/2008/01/how-to-create-generic-list-of-anonymous.html

, .

var anonymousTypeExample = new { startYear = 2000, endYear = 2000, model = "test" };
var list = (new[] { anonymousTypeExample }).ToList();

list.Clear();

list.Add(new{
    startYear = 1990,
    endYear = 2004,
    model = "Honda"
});
list.Add(new
{
    startYear = 1989,
    endYear = 1989,
    model = "KTM"
});
list.Add(new
{
    startYear = 1989,
    endYear = 1989,
    model = "KTM"
});

list = list.OrderBy(l => l.startYear).ToList();

foreach(var l in list)
{
    <div>@l.model @l.startYear - @l.endYear</div>
}
0

All Articles