Yet more LINQ woes

Posted on 2014-07-17
Last Modified: 2016-02-15
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

        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

        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();

Open in new window

Question by:John Bolter
    LVL 74

    Accepted Solution

    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:'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?
    LVL 62

    Assisted Solution

    by:Fernando Soto
    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.

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
    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…

    758 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

    11 Experts available now in Live!

    Get 1:1 Help Now