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

  • SEARCH
  • Home
  • 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 8589477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:03:59+00:00 2026-06-11T23:03:59+00:00

Good morning. Let me show you some code: //Db is a database of Tag

  • 0

Good morning.

Let me show you some code:

    //Db is a database of Tag objects, sort of
    Db db;   
    string code = "123";
    var selectedTag = db.Select(new Predicate<Tag>(tag => tag.Code == code));

    //...

    class Db
    {
        //A query method, accepting a Predicate
        public Tag Select(Predicate<Tag> predicate)
        {
            /* Here, using Intellisense, I see that Target property is a Tag object,
              but I can't cast it to Tag! Why??
            */
            var t = predicate.Target as Tag; //Always null!
        }
    }

I basically have a simple database of Tag object; when a caller invoke Select method, I would retirieve Tag object istance from predicate, but actually I can’t get it work.
Using Intellisens in debug, when I am in Select method I clearly see that predicate.Target is referenced to my Tag object, but I don’t know how to retrieve it.
Any ideas?
Thank you.

EDIT
If I try to unsafely cast predicate.Target to Tag, this way:

var t = (Tag)predicate.Target;

I get an InvalidCastException; ok, but Exception message says I cannot cast object of type DbTests to type Tag.
DbTests is the NUnit test class I’m using for my testing purpose (obviuosly…:).
Weird!
EDIT 2
Typo in line:

var selectedTag = db.Select(new Predicate<Tag>(tag => tag.Code == code));
  • 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-06-11T23:04:01+00:00Added an answer on June 11, 2026 at 11:04 pm

    Your Select method takes a delegate (the type of that delegate is a Predicate<T>, more specifically Predicate<Tag>). that delegate points to an instance and a method. What you’ve passed in for the delegate is a lambda tag => tag.Code == code. That lambda causes the compiler to generate a method for this code. That method is either contained on the instance that calls Select, or in a new class. If the generated method is static, there will be no instance and Target will be null. In any case that class that contains that method is not Tag (unless the call to Select is within the Tag class; but you didn’t detail that). So, Target cannot be a Tag object and can never be cast to Tag successfully.

    The predicate that select receives should be given a Tag object. For example:

    public Tag Select(Predicate<Tag> predicate)
    {
        Tag tag = new Tag();
        bool result = predicate(tag);
        return tag;
    }
    

    it’s not clear what you really want to do within Select; but Predicate<Tag> just returns true or false depending on the Tag given to it.

    Update:

    In your example, it’s as if you wrote this:

    public class DbTests
    {
        public void Test1()
        {
            Db db = new Db();
            Tag selectedTag = db.Select((Predicate<Tag>) TestTag);
        }
    
        private bool TestTag(Tag tag)
        {
            return tag.Code == "123";
        }
    }
    

    There is no Tag instance anywhere in the above snippet.

    Update:

    If you used an instance method from the containing class, for example:

    public class DbTests
    {
        public string code = "123";
        public void Test1()
        {
            Db db = new Db();
            Tag selectedTag = db.Select(tag => tag.Code == code);
        }
    }
    

    Then the predicate sent to select will have an instance associated with it and thus Target will not be null. In the above code, Target would be of type DbTest But, if you put the call to Select within TagData (e.g. DbTests was renamed to TagData) then Target would be of type TagData and show you instance members from TagData and you’d get what you see in your screen capture.

    Select doesn’t seem like the right term for a method that uses a Predicate<T>

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good morning. I've been having this issue for some days and have been lokking
Good Morning, i have some Trouble with the Zend Framework and Zend_DB_Select, i want
Good Morning, I have copied this code from somewere <script type=text/javascript> $(function() { setInterval(rotateImages(),
Good morning everyone! Below is a piece of code I stitched together: I used
Good morning, I finally managed to store the Facebook usernames in my database after
Good Morning! Given: public class FooClass { public void FooMethod() { using (var myEntity
Good morning, experts I need your advice. Let's say we have user , profile
good morning programers, I have this small code which content a news control panel
Good morning, I am in need of some advice, I am using a networkStream,
Good morning, Let's imagine I have a list of Tuple elements, and a function

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.