[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Yet more LINQ woes

Posted on 2014-07-17
2
Medium Priority
?
197 Views
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
        {
            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

0
Comment
Question by:John Bolter
2 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 1000 total points
ID: 40203121
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
 
LVL 64

Assisted Solution

by:Fernando Soto
Fernando Soto earned 1000 total points
ID: 40203724
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

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

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

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.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

873 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