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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:54:47+00:00 2026-05-14T02:54:47+00:00

Update Thanks to Marc’s help the AlphaPagedList class is now available on CodePlex if

  • 0

Update

Thanks to Marc’s help the AlphaPagedList class is now available on CodePlex if anyone is interested

Original

I’m trying to create an expression tree to return elements that start with a given charecter.

IList<char> chars = new List<char>{'a','b'};
IQueryable<Dept>Depts.Where(x=> chars.Contains(x.DeptName[0]));

I want this to be used on any IEnumerable where I provide a lamdba to the property to select on eg:

Depts.Alpha(x=>x.DeptName, chars);

I’ve been trying this but having no luck at all, any help?

public static IQueryable<T> testing<T>(this IQueryable<T> queryableData, Expression<Func<T,string>> pi, IEnumerable<char> chars)
{
// Compose the expression tree that represents the parameter to the predicate.

ParameterExpression pe = Expression.Parameter(queryableData.ElementType, "x");
ConstantExpression ch = Expression.Constant(chars,typeof(IEnumerable<char>));
// ***** Where(x=>chars.Contains(x.pi[0])) *****
// pi is a string property
//Get the string property

Expression first = Expression.Constant(0);
//Get the first character of the string
Expression firstchar = Expression.ArrayIndex(pi.Body, first);
//Call "Contains" on chars with argument being right
Expression e = Expression.Call(ch, typeof(IEnumerable<char>).GetMethod("Contains", new Type[] { typeof(char) }),firstchar);


MethodCallExpression whereCallExpression = Expression.Call(
    typeof(Queryable),
    "Where",
    new Type[] { queryableData.ElementType },
    queryableData.Expression,
    Expression.Lambda<Func<T, bool>>(e, new ParameterExpression[] { pe }));
// ***** End Where *****

return (queryableData.Provider.CreateQuery<T>(whereCallExpression));
}
  • 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-14T02:54:47+00:00Added an answer on May 14, 2026 at 2:54 am

    Something like (EDITED after re-reading the question) – but note that Expression.Invoke doesn’t work on EF in 3.5SP1 (but it is fine in LINQ-to-SQL):

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Linq.Expressions;
    
    class Dept
    {
        public string DeptName { get; set; }
    }
    public static class Program
    {
        static void Main()
        {
            IList<char> chars = new List<char>{'a','b'};
            Dept[] depts = new[] { new Dept { DeptName = "alpha" }, new Dept { DeptName = "beta" }, new Dept { DeptName = "omega" } };
            var count = testing(depts.AsQueryable(), dept => dept.DeptName, chars).Count();
        }
    
        public static IQueryable<T> testing<T>(this IQueryable<T> queryableData, Expression<Func<T,string>> pi, IEnumerable<char> chars)
        {
            var arg = Expression.Parameter(typeof(T), "x");
            var prop = Expression.Invoke(pi, arg);
            Expression body = null;
            foreach(char c in chars) {
                Expression thisFilter = Expression.Call(prop, "StartsWith", null, Expression.Constant(c.ToString()));
                body = body == null ? thisFilter : Expression.OrElse(body, thisFilter);
            }
            var lambda = Expression.Lambda<Func<T, bool>>(body ?? Expression.Constant(false), arg);
            return queryableData.Where(lambda);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

UPDATE: Thanks for all your help guys! I just need to take a litte
Update: Thanks everbody, things are working 100% now with this query: SELECT e.entry_id FROM
UPDATE: Thanks to Sifu Bill's advice I have amended the SQL query. Now it
UPDATE: Thanks to everyone for the responses. I didn't realize document.write() was deprecated. Add
Update: Thanks for the suggestions guys. After further research, I’ve reformulated the question here:
UPDATE: Solved. Thanks BusyMark! EDIT: This is revised based on the answer below from
Update: Now that it's 2016 I'd use PowerShell for this unless there's a really
Update Thanks to @mrtsherman verifying my code was fine, I dove into the CSS
Update 2: thanks again to @deepak-azad, I managed to solve my problem : here
Where do I find the javax.crypto source code? --update Thanks for the OpenJdk version,

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.