Solved

Help with some (not simple) LINQ

Posted on 2014-09-09
1
126 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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

744 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

8 Experts available now in Live!

Get 1:1 Help Now