Solved

Convert Dictionary foreach  search to Linq

Posted on 2009-05-13
11
913 Views
Last Modified: 2013-12-17
Hi

is it possible to convert this code to use Linq instead?
private bool Exists(string name)

        {

            foreach (ClientData p in _clients.Keys)

            {

                if (p.Id.Equals(name, StringComparison.OrdinalIgnoreCase))

                {

                    return true;

                }

            }
 

            return false;

        }

Open in new window

0
Comment
Question by:AWestEng
  • 4
  • 3
  • 3
11 Comments
 
LVL 39

Accepted Solution

by:
abel earned 400 total points
ID: 24377004
Yes, it is (btw, there's a LINQ zone too, you may have more luck there...) Here's a one-liner, but you may want to split it a bit:

private bool Exists(string name)

{

    return (from p in _clients.Keys 

        where p.Id.Equals(name, StringComparison.OrdinalIgnoreCase) 

        select p).FirstOrDefault() != null;

}

Open in new window

0
 
LVL 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 100 total points
ID: 24377422
Hi AWestEng;

Here is code snippet showing how it can be done using Linq methods.

Fernando
using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;
 

namespace WindowsFormsApplication13

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }
 

        List<ClientData> _clients = new List<ClientData>();
 

        private void Form1_Load(object sender, EventArgs e)

        {

            ClientData cd = new ClientData() { Id = "John" };

            _clients.Add(cd);

            cd = new ClientData() { Id = "William" };

            _clients.Add(cd);

            cd = new ClientData() { Id = "AWestEng" };

            _clients.Add(cd);

            cd = new ClientData() { Id = "Alice" };

            _clients.Add(cd);

        }
 

        private void button1_Click(object sender, EventArgs e)

        {

            // User types name in text box and copies to name

            string name = textBox1.Text.Trim();
 

            if (!Exists(name))

            {

                MessageBox.Show("Name was NOT found");

            }

            else

            {

                MessageBox.Show("Name was found");

            }

        }
 

        private bool Exists(string name)

        {

            return _clients.Where(c => c.Id.Equals(name, StringComparison.OrdinalIgnoreCase))

                           .Select(c => c.Id == String.Empty ? false : true).FirstOrDefault();

        }

    }
 

    public class ClientData

    {

        public string Id { get; set; }

    }

}

Open in new window

0
 
LVL 1

Author Comment

by:AWestEng
ID: 24377483
sorry FernandoSoto:, I accepted able's reply you where posting yours, I'm sorry m8..
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 24377641
Not a problem, that happens. Have a great day.  ;=)
0
 
LVL 39

Expert Comment

by:abel
ID: 24377831
If you want, you can request reopen and split. I won't object to that. FernandoSoto put in quite some effort, though I believe his solution is about the same as what I showed (though slightly more verbose perhaps).
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 62

Expert Comment

by:Fernando Soto
ID: 24377994
No need to do that, it is really not a problem.  ;=)
0
 
LVL 1

Author Closing Comment

by:AWestEng
ID: 31581059
Thx guys,, perfect job
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 24395047
Thanks AWestEng and abel.  ;=)
0
 
LVL 1

Author Comment

by:AWestEng
ID: 24395269
No!  Thank you guys! Thx for always put your own time into others problem and help out.
I really appreciate it . :)
0
 
LVL 39

Expert Comment

by:abel
ID: 24396146
you're welcome, both, always around to help (almost)  :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Class object 2 26
Not showing page correctly 3 30
Visual Studio 2015 Source Code Control 10 34
Where can I ask about my ASP.NET MVC project? 6 21
A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

920 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

15 Experts available now in Live!

Get 1:1 Help Now