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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:59:39+00:00 2026-05-31T22:59:39+00:00

My site has a bunch of widgets and i’m trying to filter them based

  • 0

My site has a bunch of widgets and i’m trying to filter them based on the url which is passed in. Say a Widget has the following structure:

public class Widget {
    public int Id { get; set; }
    public string Name { get; set; }
    public string Urls { get; set; }
}

Where Urls is a comma separated list for the urls where the widget should be displayed, e.g.:

/, /Blog/, /Blog/123, /News/*

The asterisk after News indicates the Widget will be selected whenever the passed in url starts with /News/.

How could i modify the following method to filter the widgets based on my conditions above?

public IList<Widget> GetWidgets(string url) {
    return _session
        .Where(w => w.Urls.Contains(url))
        .ToList();
}

Ideally i’d like to use a linq query and it must only hit the database once. I’d appreciate the help. Thanks

  • 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-31T22:59:40+00:00Added an answer on May 31, 2026 at 10:59 pm

    I managed to solve this by adding my own wild card match generator. See http://sentinel101.wordpress.com/2010/12/30/extend-nhibernate-linq-for-regex-matching/ for example of how to register the generator. Here’s the generator incase anyone is interested:

    public class WildCardMatchGenerator : BaseHqlGeneratorForMethod {
        public WildCardMatchGenerator() {
            var methodDefinition = ReflectionHelper.GetMethodDefinition(() => WildCardMatchExtensions.WildCardMatch(null, null, ','));
            SupportedMethods = new[] { methodDefinition };
        }
    
        public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor) {
            return treeBuilder.Equality(treeBuilder.MethodCall("[dbo].[WildCardMatch]", new[] {
                visitor.Visit(arguments[0]).AsExpression(),
                visitor.Visit(arguments[1]).AsExpression(),
                visitor.Visit(arguments[2]).AsExpression()
            }), treeBuilder.Constant(1));
        }
    }
    

    And here is the WildCardMatch UDF:

    CREATE FUNCTION [dbo].[WildCardMatch] (
        @Pattern NVARCHAR(MAX),
        @Input NVARCHAR(MAX),
        @Separator NVARCHAR(5)
    )
    RETURNS BIT
    AS
    BEGIN
        SET @Pattern = REPLACE(@Pattern, '*', '%')
    
        DECLARE @RtnValue BIT
        SELECT @RtnValue = CASE WHEN COUNT(*) > 0 THEN 1 ELSE 0 END FROM [dbo].[Split](@Pattern, @Separator) WHERE @Input LIKE [Data]
    
        RETURN @RtnValue
    END
    

    And the Split function it calls (from http://blogs.microsoft.co.il/blogs/itai/archive/2009/02/01/t-sql-split-function.aspx):

    CREATE FUNCTION [dbo].[Split]
    (    
        @RowData NVARCHAR(MAX),
        @Separator NVARCHAR(MAX)
    )
    RETURNS @RtnValue TABLE 
    (
        [Id] INT IDENTITY(1,1),
        [Data] NVARCHAR(MAX)
    ) 
    AS
    BEGIN 
        DECLARE @Iterator INT
        SET @Iterator = 1
    
        DECLARE @FoundIndex INT
        SET @FoundIndex = CHARINDEX(@Separator, @RowData)
    
        WHILE (@FoundIndex > 0)
        BEGIN
            INSERT INTO @RtnValue ([Data])
            SELECT Data = LTRIM(RTRIM(SUBSTRING(@RowData, 1, @FoundIndex - 1)))
    
            SET @RowData = SUBSTRING(@RowData, @FoundIndex + DATALENGTH(@Separator) / 2, LEN(@RowData))
            SET @Iterator = @Iterator + 1
            SET @FoundIndex = CHARINDEX(@Separator, @RowData)
        END
    
        INSERT INTO @RtnValue ([Data])
        SELECT Data = LTRIM(RTRIM(@RowData))
    
        RETURN
    END
    

    Lastly you’ll need the C# implementation of the above UDF:

    public static class WildCardMatchExtensions {
        public static bool WildCardMatch(this string pattern, string input, char separator = ',') {
            foreach (var str in pattern.Split(new char[] { separator }, StringSplitOptions.RemoveEmptyEntries)) {
                if (Regex.IsMatch(input, Regex.Escape(str.Trim()).Replace("\\*", ".*")))
                    return true;
            }
    
            return false;
        }
    }
    

    Hope this helps.

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

Sidebar

Related Questions

I'm currently building a site which has a bunch of main categories and in
I have the following situation: I have site A, which has it's Mercurial repo,
My site has an input box, which has a onkeydown event that merely alerts
I have a page on my site which has multiple drop down boxes as
I have an authenticated directory in my site that has a bunch of directories
I have a bunch of existing scripts for a site which use gd library
I have an existing site that has a bunch of different models and controllers.
I have a bunch of static content on a site that has always lived
I am trying to make the following design for a web site. The last
I'm building a site (with jQuery 1.4) which has a floating (fixed position) basket.

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.