Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6868457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:26:12+00:00 2026-05-27T03:26:12+00:00

I need to reflect a property for lambda for my Repository, here’s the code:

  • 0

I need to reflect a property for lambda for my Repository, here’s the code:

public abstract class RestController<T> : Controller where T : class
{
    private readonly IRepository _db;
    private readonly string _identityProp;

    protected RestController(IRepository db, string identityProp)
    {
        _db=db;
        _identityProp = identityProp;
    }

    protected virtual void Delete(T item)
    {
        var value = item.GetType().GetProperty(_identityProp).GetValue(item, null);
        var items = _db.All<T>()
            .Where(i=>i.GetType().GetProperty(_identityProp)==val)
            .ToList();
        items.ForEach(x=>_db.Delete(x));
        _db.CommitChanges();
        return Json("success");
    }
}

but the result of lambda is an empty list… Help please, what I’m doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:26:12+00:00Added an answer on May 27, 2026 at 3:26 am

    You are missing a .GetValue(i, null) in your LINQ query. Currently, you are comparing the value of the property (value) with the PropertyInfo describing the property.

    Correct code:

    var items = _db.All<T>()
                   .Where(i => i.GetType().GetProperty(_identityProp)
                                          .GetValue(i, null) == val)
                   .ToList();
    

    Or, a little bit more performance orientated:

    var propertyInfo = item.GetType().GetProperty(_identityProp);
    var value = propertyInfo.GetValue(item, null);
    var items = _db.All<T>()
                   .Where(i => propertyInfo.GetValue(i, null) == val)
                   .ToList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the domain that I wish to have: public class Person { public
I have a class Foo that a property of the type Bar. public class
Need some help about with Memcache. I have created a class and want to
I am writing a tool where I need to reflect upon methods and if
The language shortcut public string Code { get; set; } saves a bit of
I have a class which looks like as follows: public class MyConstants { public
I bound to class' get only properties (for example) public bool IsRunning { get
public class MyData { public String id_number,first_name,last_name,age,level; public MyData(String raw_string){ String[] parameters = raw_string.split(,);
I need to rename it and that should reflect in all the dependency objects.
Need a function that takes a character as a parameter and returns true if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.