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 @_ @?

6
  • why not select new { c.person_name} ? Commented Oct 12, 2016 at 9:29
  • 1
    Look at the Expression class Commented Oct 12, 2016 at 9:30
  • @Mostafiz cause that string is coming dynamically and is gonna be different every time @_ @ Commented Oct 12, 2016 at 9:32
  • Have you tried c.customSelect? Commented Oct 12, 2016 at 9:54
  • @gayan1991 yeah, it says that my db model does not contains definition of fk Commented Oct 12, 2016 at 10:09

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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
@IvanStoev, no it is possible to create dynamic type at runtime
@AdilMammadov It's possible, but hard. And not anonymous type which is compiler feature.
@IvanStoev, I agree with that it is hard, especially, when you add properties instead of fields (link) :).
@AdilMammadov Yeah :) Dynamic LINQ has a handy DynamicExpression.CreateClass() method which does all that, and also implements correctly Equals and GetHashCode plus the caching.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.