Solved

Help with some (not simple) LINQ

Posted on 2014-09-09
1
147 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

628 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