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 8773111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:12:42+00:00 2026-06-13T18:12:42+00:00

With the ListBox control it is possible to feed it a DataSource, name a

  • 0

With the ListBox control it is possible to feed it a DataSource, name a DisplayMember and a ValueMember and through some magic it will display a field from the DataSource and return a selected ValueMember. It can work wit a linq-to-sql result without even knowing anyting specific about the table it is feed with.

Isn’t Reflection and Attributes doing some magic? How does it work!
I have a need to do something similar but I do not know where to start. I’m a beginner for LINQtoSQL.

This is what I want to do.
I have a source table that I want to filter. The source table can be anything but will be originating from some DataContext.

var MySourceTable =
    from MyRecord in Context.GetTable<MySourceTable>()
    select new
    {
        Value = MyRecord.ID,
        Display = MyRecord.Name,
        FilterValue = MyRecord.Value
    };

In my control I want to be able to filter MySourceTable on some given value. The control does not know what table is used (MySourceTable in the example above) and the control does only know the three names, ID, Name and Value of the fields in the record it should use.

The filter query should look like the example below.

var MyTable
    from Record in MySourceTable
    where FilterValue == GivenValue
    select new
    {
        Value = Record.ID,
        Display = Record.Name,
    };

Can somebody advise me on where to start?

  • 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-13T18:12:45+00:00Added an answer on June 13, 2026 at 6:12 pm

    I wrote a filter engine that takes in a Property and Value as a string, and is able to use that as a where clause.

    IQueryable<T> FilterFunction<T>(IQueryable<T> query)
    {
        ParameterExpression p = Expression.Parameter(typeof(T), "notused");
    
        Expression<Func<T, bool>> wherePredicate =
          Expression.Lambda<Func<T, bool>>(
              Expression.Equal(
                Expression.Call(Expression.Property(p, FilterProperty), "ToString", new Type[0]),
                Expression.Constant(FilterValue)), p);
    
        return query.Where(wherePredicate);
    }
    

    You should be able to pass in an Expression<Func<T, TResult>> built in a similar way into query.Select()

    If I am understanding your question correctly, I believe this will work:

    string DisplayProperty = "Name";
    string ValueProperty = "ID";
    
    IQueryable<Record> SelectRecordProperties<T>(IQueryable<T> query)
    {
        ParameterExpression p = Expression.Parameter(typeof(T), "notused");
    
        MethodInfo ctorMethod = typeof(Record).GetMethod("Create");
    
        Expression<Func<T, Record>> selectPredicate =
          Expression.Lambda<Func<T, Record>>(
            Expression.Call(ctorMethod,
                Expression.PropertyOrField(p, DisplayProperty),
                Expression.PropertyOrField(p, ValueProperty)), p);
    
        return query.Select(selectPredicate);
    }
    class Record
    {
        public static Record Create(string display, string value)
        {
            return new Record() { Display = display, Value = value };
        }
        public object Display { get; set; }
        public object Value { get; set; }
    }
    

    So for your full function you’d need to combine these two ideas so that your filtering works.

    By the way, there are many possible ways to build the expression tree for this, there was some tool I’ve found at one point which would show you the expression tree I think, so you could manually write the linq query and see how .Net builds the expression, then modify this code to build it based on that to possibly get a more efficient expression tree.

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

Sidebar

Related Questions

Is it possible to have a listbox displaying its items with a control template
I have some control like <ListBox ItemsSource={Binding tests, UpdateSourceTrigger=PropertyChanged} ItemTemplate={StaticResource TestTemplate} /> When user
Possible Duplicate: transfer data from javascript popup multiline textbox to a select control I
I have a ListBox control, that contains a few items that display attachments (files
I have a user control that inherits from the ListBox class and displays a
Is it possible to have a listbox (or another control that has a SelectedItem)
Possible Duplicate: Cross-thread operation not valid: Control accessed from a thread other than the
How can I move items from one list box control to another listbox control
I have created a listbox control with following DataTemplate <DataTemplate x:Key=lb_Itemtemplate> <DockPanel> <TextBlock Text={Binding}
Curve the border of silverlight listbox control: I just want to curve the ends

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.