David Clarke
David Clarke

Reputation: 13256

Refactor linq statement

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: [email protected]|" },
    new { displayName = "Hamburger Pattie",  summary = "|Title: Network Development Engineer|Location: |Department: Planning|Email: [email protected]|" },
};

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: [email protected]...
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();

Upvotes: 1

Views: 340

Answers (3)

Joe
Joe

Reputation: 11637

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.

Upvotes: 1

Christian Payne
Christian Payne

Reputation: 7169

(I know this doesn't answer your specific question), but here's my contribution anyway:

You haven't really described how your data comes in. As @Joe suggested, you could use a regex or split the fields as I've done below.

Either way I would suggested refactoring your code to allow unit testing.

Otherwise if your data is invalid / corrupt whatever, you will get a runtime error in your linq query.

    [TestMethod]
    public void TestMethod1()
    {
        var matches = new[] 
        {
            new { displayName = "Sausage Roll", summary = "|Title: Network Coordinator|Location: Best Avoided|Department: Coordination|Email: [email protected]|" },
            new { displayName = "Hamburger Pattie",  summary = "|Title: Network Development Engineer|Location: |Department: Planning|Email: [email protected]|" },
        };

        IList<Person> persons = new List<Person>();
        foreach (var m in matches)
        {
            string[] fields = m.summary.Split('|');
            persons.Add(new Person { displayName = m.displayName, Title = fields[1], Location = fields[2], Department = fields[3] });
        }

        Assert.AreEqual(2, persons.Count());
    }

    public class Person
    {
        public string displayName { get; set; }
        public string Title { get; set; }
        public string Location { get; set; }
        public string Department { get; set; }
        /* etc. */
    }

Upvotes: 2

saus
saus

Reputation: 2174

like this?

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

Upvotes: 0

Related Questions