Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Help with some (not simple) LINQ

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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

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…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Integration Management Part 2
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

824 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