0

Is there any way to customize it?

This is what Im trying to do:

string customSelect = "c.person_name";
int per = PersonID();
var RetrievItem = (from c in db.person where c.person_id == per select new { customSelect }).FirstOrDefault();

I've tried to debugging it but it just ended up retrieving given string instead of value from database

Any suggestions @_ @?

Harugawa
  • 539
  • 5
  • 19

1 Answers1

3

In addition to Hamlet's comment about an expression based solution, you can have a look at the Dynamic Linq library. It supports lamba expressions defined as strings, which is exactly what you need.

bkaf
  • 476
  • 6
  • 16
  • I would rather say this is the only possible dynamic select solution so far because you cannot create anonymous types at runtime, which Dynamic LINQ solves by creating dynamic classes at runtime. +1 – Ivan Stoev Oct 12 '16 at 09:55
  • @IvanStoev, no it is possible to create dynamic type at runtime – Adil Mammadov Oct 12 '16 at 10:53
  • @AdilMammadov It's possible, but hard. And not **anonymous** type which is compiler feature. – Ivan Stoev Oct 12 '16 at 11:05
  • @IvanStoev, I agree with that it is hard, especially, when you add *properties* instead of *fields* ([link](https://msdn.microsoft.com/en-us/library/2sd82fz7(v=vs.110).aspx)) :). – Adil Mammadov Oct 12 '16 at 11:11
  • 1
    @AdilMammadov Yeah :) Dynamic LINQ has a handy `DynamicExpression.CreateClass()` method which does all that, and also implements correctly `Equals` and `GetHashCode` plus the caching. – Ivan Stoev Oct 12 '16 at 11:20