Solved

Help with some (not simple) LINQ

Posted on 2014-09-09
1
129 Views
Last Modified: 2016-02-15
Hi, more LINQ woes. I really would appreciate some help. My code that doesn't work is below.
Thank you
John

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

namespace ConsoleApplication348
{
    class Program
    {
        static void Main(string[] args)
        {
            List<String> numbers = new List<String> { "one", "one", "one", "two", "three" };

            String searchNumber = "one";
            var found1 = (
                            from x
                                in numbers
                            where x == searchNumber
                            select new { x }
                          ).ToList().Take(1);       //I want found1 to be a string and have value 'one'
            



            searchNumber = "twenty";
            var found20 = (
                            from x
                                in numbers
                            where x == searchNumber
                            select new { x }
                           ).ToList().Take(1).DefaultIfEmpty("not found");  //I want found20 to be a string and have value 'not found'

        }
    }
}

Open in new window

0
Comment
Question by:John Bolter
1 Comment
 
LVL 1

Accepted Solution

by:
Michael O'Shea earned 500 total points
ID: 40313183
Hi again John, here's some template code.
            List<String> numbers = new List<String> { "one", "one", "one", "two", "three" };

            var found1 = (
                from x
                    in numbers
                where x == "one"
                select x
                ).DefaultIfEmpty("ONE NOT FOUND").First();


            var found20 = (
                from x
                    in numbers
                where x == "twenty"
                select x
                ).DefaultIfEmpty("TWENTY NOT FOUND").First();

Open in new window

hth
Mike
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now