Yet more LINQ woes

Hi, more LINQ. I'm sorry, I just can't get the hang of it, even with simple stuff.
Here is the whole test program (that won't compile). I just can't sort out the LINQ syntax. Any help most appreciated.
Thank you
John B

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

namespace ConsoleApplication4
{
    class Program
    {
        enum WORD
        {
            HELLO,
            GOODBYE,
            ONE,
            TWO,
            THREE,
        }

        KeyValuePair<WORD, String>[] words = { 
                                        new KeyValuePair<WORD, String>(WORD.HELLO, "Hello"),
                                        new KeyValuePair<WORD, String>(WORD.HELLO, "Bonjour"),
                                        new KeyValuePair<WORD, String>(WORD.ONE, "1"),
                                        new KeyValuePair<WORD, String>(WORD.ONE, "ein"),
                                        new KeyValuePair<WORD, String>(WORD.TWO, "zwei"),
                                        new KeyValuePair<WORD, String>(WORD.THREE, "drei"),
                                        };

        private Boolean found(WORD word)
        {
            return (
                    from xxx in words
                      group xxx by xxx.Key into grouped
                        where grouped.Count() > 0
                        where xxx.Key = word
                           select true
                   ).DefaultIfEmpty(false);
        }

        public void Run()
        {
            Console.WriteLine(found(WORD.ONE));     //TRUE
            Console.WriteLine(found(WORD.TWO));     //TRUE
            Console.WriteLine(found(WORD.THREE));   //TRUE
            Console.WriteLine(found(WORD.GOODBYE)); //FALSE
            Console.WriteLine(found(WORD.HELLO));   //TRUE
        }

        static void Main(string[] args)
        {
            Program program = new Program();
            program.Run();
        }
    }
}

Open in new window

John BolterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
First you need to understand that when you're dealing with LINQ 99.99999% of the time the return type of your query is going to be IEnuermable<XXXX>, where XXXX is an arbitrary type. What that means for your current code is that you declared your method to return a Boolean, but you are attempting to return an IEnumerable<Boolean> from your query. I'm sure you see the hang up there.

Next, your query won't compile because in the second where clause you are trying to reference the wrong variable. Instead of xxx.Key you should have grouped.Key. Only a grouping would have a Key. Also, you've got an assignment operator ( = ) where you should have an equality operator ( == ).

So my question to you would be:  What is this code *supposed* to do?


Also, I've written an introductory article to LINQ:  http://www.experts-exchange.com/Programming/Languages/.NET/A_10170-A-Beginner's-Guide-to-LINQ.html . As it stands, there is no part 2. Perhaps you might help me determine my next topic if you can tell me what it is about LINQ that is difficult for you. In other words, what things pertaining to LINQ really trip you up?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fernando SotoRetiredCommented:
Hi John;

I have made some corrections to your function found / the Linq query so that it works as you had intended it to work. We may be able to make it work better if you Can explain more of what you are attempting to accomplish in the application.

private Boolean found(WORD word)
{
    return (
            // Query through the collection of KVP 
            from xxx in words
            // Group on the the Key of the KVP
            group xxx by xxx.Key into grouped
            // This where clause is NOT needed because every group will have at lease 1 KVP in it
            where grouped.Count() > 0
            // You do not want to change the value of grouped.Key but only compare it to word
            // so you need the == and not =.
            where grouped.Key == word
            // If there is a value up to this point then return true
            select true
            // Because a Linq query will return a collection normally of xxx but in this case a collection
            // of boolean true and you only need one boolean to be returned we use the FirstOrDefault method
            // to return one value. If the collection will return and empty collection when word was not found
            // FirstOrDefault will return false because the select would have returned a boolean and the default
            // is false.
           ).FirstOrDefault();            
}

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.