Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Help with some (not simple) LINQ

Posted on 2014-09-09
1
Medium Priority
?
154 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

730 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