Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pimp my LINQ: a learning exercise based upon another post

I decided to try out LINQ for the first time to try and solve this question.

The results of my first foray into the wonderful world of LINQ looked like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> list = new List<string>() 
            { 
               "fred-064528-NEEDED1", 
               "xxxx", 
               "frederic-84728957-NEEDED2", 
               "sam-028-NEEDED3", 
               "-----", 
               "another-test" 
            };

            var result =
            from s in list
            where (from c in s where c == '-' select c).Count() == 2
            select s.Substring(s.LastIndexOf("-") + 1);

            foreach (string s in result)
                Console.WriteLine(s);
            Console.WriteLine("Press Enter");
            Console.ReadLine();
        }
    }
}

I'd like to know how I can improve the above solution to this contrived little example. I'm not too interested in whether I've used the best validation method, or how I could localise "Press Enter" or anything like that; I'm just interested in using this example to learn a little more about LINQ.

like image 643
Matt Jacobsen Avatar asked Aug 10 '10 11:08

Matt Jacobsen


4 Answers

var result =
        from s in list
        where s.Count(x => x == '=') == 2
        select s.Substring(s.LastIndexOf("-") + 1);
like image 71
nothrow Avatar answered Oct 22 '22 00:10

nothrow


It can also be written using Lambda expressions:

var result =
            list.Where(s => (from c in s where c == '-' select c).Count() == 2).Select(
                s => s.Substring(s.LastIndexOf("-") + 1));

I prefer Lambda expressions over LINQ syntax because of the Fluent interface. IMHO it is more human readable.

like image 4
heads5150 Avatar answered Oct 22 '22 01:10

heads5150


I don't think this is an improvement, as it is less readable, but you can do it all in one-line using some of the inbuilt methods in the List class:

list.FindAll(s => s.ToCharArray().
    Where(c => c == '-').Count() ==2).
    ForEach(c => Console.WriteLine(c.Substring(c.LastIndexOf("-") + 1)));

Personally I find this rather horrible, so it's just for interest!

like image 4
Dan Diplo Avatar answered Oct 22 '22 01:10

Dan Diplo


This is pretty nice I think. Partly LINQ.

var result = String.Join("-", inputData.Split('-').Skip(2));

If there can't be any '-' after the first two then this will do (not LINQ):

var result = inputData.Split('-')[2];  //If the last part is NEE-DED then only NEE is returned. And will fail on wrong input
like image 4
Lasse Espeholt Avatar answered Oct 22 '22 00:10

Lasse Espeholt