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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:59:57+00:00 2026-05-20T04:59:57+00:00

I have a linq expression that I’ve been playing with in LINQPad and I

  • 0

I have a linq expression that I’ve been playing with in LINQPad and I would like to refactor the expression to replace all the tests for idx == -1 with a single test. The input data for this is the result of a free text search on a database used for caching Active Directory info. The search returns a list of display names and associated summary data from the matching database rows. I want to extract from that list the display name and the matching Active Directory entry. Sometimes the match will only occur on the display name so there may be no further context. In the example below, the string “Sausage” is intended to be the search term that returned the two items in the matches array. Clearly this wouldn’t be the case for a real search because there is no match for Sausage in the second array item.

var matches = new [] 
{
    new { displayName = "Sausage Roll", summary = "|Title: Network Coordinator|Location: Best Avoided|Department: Coordination|Email: Sausage.Roll@somewhere.com|" },
    new { displayName = "Hamburger Pattie",  summary = "|Title: Network Development Engineer|Location: |Department: Planning|Email: Hamburger.Pattie@somewhere.com|" },
};

var context = (from match in matches
                let summary = match.summary
                let idx = summary.IndexOf("Sausage")
                let start = idx == -1 ? 0 : summary.LastIndexOf('|', idx) + 1
                let stop = idx == -1 ? 0 : summary.IndexOf('|', idx)
                let ctx = idx == -1 ? "" : string.Format("...{0}...", summary.Substring(start, stop - start))
                select new { displayName = match.displayName, summary = ctx, })
                .Dump();

I’m trying to create a list of names and some context for the search results if any exists. The output below is indicative of what Dump() displays and is the correct result:

displayName        summary 
----------------   ------------------------------------------
Sausage Roll       ...Email: Sausage.Roll@somewhere.com...
Hamburger Pattie

Edit: Regex version is below, definitely tidier:

Regex reg = new Regex(@"\|((?:[^|]*)Sausage[^|]*)\|");      
var context = (from match in matches
                let m = reg.Match(match.summary)
                let ctx = m.Success ? string.Format("...{0}...", m.Groups[1].Value) : ""
                select new { displayName = match.displayName, context = ctx, })
                .Dump();
  • 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-20T04:59:57+00:00Added an answer on May 20, 2026 at 4:59 am

    Or something like this:

        Regex reg = new Regex(@"^|Email.*|$");
        foreach (var match in matches)
        {
            System.Console.WriteLine(match.displayName + " ..." + reg.Match(match.summary) + "... ");
        }
    

    I haven’t tested this, probably not even correct syntax but just to give you an idea of how you could do it with regex.

    Update
    Ok, i’ve seen your answer and it’s good that you posted it because I think i didn’t explain it clearly.
    I expected your answer to look something like this at the end (tested using LINQPad now, and now i understand what you mean by using LINQPad because it actually does run a C# program not just linq commands, awesome!) Anyway this is what it should look like:

    foreach (var match in matches)
        Console.WriteLine(string.Format("{0,-20}...{1}...", match.displayName, Regex.Match(match.summary, @"Email:(.*)[|]").Groups[1]));
    }
    

    That’s it, the whole thing, take linq out of it, completely!
    I hope this clears it up, you do not need linq at all.

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

Sidebar

Related Questions

Can a LINQ expression replace all cases where regex would have previously been used?
I am doing some performance tests and noticed that a LINQ expression like result
I have a Linq expression that (for northwind) gets the total qty ordered per
I have the following LINQ expression that's not returning the appropriate response var query
I have the following linq expression pulling all data from my database: var items
I have a GridView what I fill through a LINQ expression. Something like this:
I have a Linq expression that operates on a list of objects for which
I have a linq expression that returns transactions in groups. Each transaction has a
In F# I have a function that returns System.Linq.Expression instances: and System.Object with member
Suppose I have an IQueryable<T> expression that I'd like to encapsulate the definition of,

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.