Click here to Skip to main content
15,886,830 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a text file that compares its customer ID with a dictionary of Customer ID and State from the database. After it does the compare I need to compile every state with its Customer ID into a List String so I can use that again to check additional details in my database. If someone has an example I can use?

What I have tried:

if (!mapping.ContainsKey(ssn))
                    {
                        newfile += line + Environment.NewLine;
                        
                    }
                    else
                    {

                        List<String> x = mapping.Where(w => w.Value.Contains("TX"))
                            .Select(n => n.Value).ToList();

                    }      


mapping is the name of my dictionary, but so far this doesn't seem to work. I need to add the results of the compare to a list.
Posted
Updated 23-Mar-17 6:20am
v2
Comments
Karthik_Mahalingam 23-Mar-17 11:54am    
Contains will return bool value, you cannot assign it to string type list
SmartDeveloping 23-Mar-17 12:02pm    
any other way I can check a Value from a dictionary?
Karthik_Mahalingam 23-Mar-17 12:04pm    
your question is not so clear, confusing.
SmartDeveloping 23-Mar-17 12:07pm    
I have one dictionary using that I am comparing with Textfile. Something like this:
if (!mapping.ContainsKey(custID))
{
newfile += line + Environment.NewLine;

}
else
{
//Check which state and add to List String?

Be a smart developer and check the documentation: Dictionary(TKey, TValue).ContainsKey Method (TKey) (System.Collections.Generic)[^]
 
Share this answer
 
Comments
SmartDeveloping 23-Mar-17 11:29am    
This is for a dictionary, I am trying to add my results to a List String now.

// ContainsKey can be used to test keys before inserting
// them.
if (!openWith.ContainsKey("ht"))
{
openWith.Add("ht", "hypertrm.exe");
Console.WriteLine("Value added for key = \"ht\": {0}",
openWith["ht"]);
}
else
List String (Add the results)

Richard MacCutchan 23-Mar-17 11:59am    
I am still not sure where the value "ht" comes from or which list you are trying to update. Your original question is far from clear.
SmartDeveloping 23-Mar-17 12:08pm    
Its an example from the link you provided.
Richard MacCutchan 23-Mar-17 12:24pm    
So how does that relate to your problem?
Here is an example based on your comment and original question

C#
Dictionary<string, int> mapping =
new Dictionary<string, int>();

mapping.Add("cat", 2);
mapping.Add("dog", 1);
mapping.Add("llama", 0);
mapping.Add("iguana", -1);

List<String> x = mapping.Where(w=> w.Key.Contains("a"))
   .Select(n => n.Key).ToList();
 
Share this answer
 
Comments
SmartDeveloping 23-Mar-17 11:55am    
Bryan that's what I was looking for, just a small question. Since my dictionary already has values and I am comparing them with a textfile, does it go ahead and add the results in the List String code you provided above?
Bryian Tan 23-Mar-17 12:09pm    
I'm not clear of your question. But I'm guessing this is something you looking for?
List<string> x = mapping.Where(w=> YourTextFile.Contains(w.SomeValue))
.Select(s => s.SomeColumn).ToList();
SmartDeveloping 23-Mar-17 12:16pm    
This is what I have

f (!mapping.ContainsKey(custID))
{
newfile += line + Environment.NewLine;

}
else
{
//Check which state and add to List String?
SmartDeveloping 23-Mar-17 12:16pm    
Sorry for making this confusing.
Bryian Tan 23-Mar-17 12:40pm    
where is the State?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900