简体   繁体   English

如何在C#中使用linq查询将文本框中的值与字典进行匹配,并在按钮单击时在另一个文本框中显示它

[英]How to match value from textbox to dictionary and display it in another textbox on button click with linq query in C#

Dictionary<int, string> dictionary = new Dictionary<int, string>();
            dictionary.Add(04634, "AMBASAMUDRAM");
            dictionary.Add(04253, "ANAMALI");
            dictionary.Add(04153, "ARAKANDANALLUR");

Need linq query to get the key from textbox1 and match it to dictionary and displays the match value into textbox2 and this must be done on button click. 需要linq查询以从textbox1获取密钥并将其与字典匹配,然后将匹配值显示到textbox2中,这必须在单击按钮时完成。

The below code is used without linq. 以下代码不带linq使用。 And i need the same to done with the Linq Query. 我需要使用Linq查询完成相同的操作。

private void button1_Click(object sender, EventArgs e)
        {
            string std = DoWork(Convert.ToInt32(textBox1.Text));
            textBox2.Text = std;

        }
        public string DoWork(int stdcode)
        {
            Dictionary<int, string> dictionary = new Dictionary<int, string>();
            dictionary.Add(04634, "AMBASAMUDRAM");
            dictionary.Add(04253, "ANAMALI");
            dictionary.Add(04153, "ARAKANDANALLUR");
            dictionary.Add(04371, "ARANTANGI");
            dictionary.Add(04320, "ARAVAKURICHI");
            dictionary.Add(04329, "ARIYALUR");

            return (dictionary[stdcode]);
        }

You can use 您可以使用

var fooDict= dictionary .Where(a=> a.Value == "AMBASAMUDRAM") var fooDict = dictionary .Where(a => a.Value ==“ AMBASAMUDRAM”)

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
      // Use a dictionary with an int key.
      Dictionary<int, string> dictionary = new Dictionary<int, string>();
        dictionary.Add(04634, "AMBASAMUDRAM");
        dictionary.Add(04253, "ANAMALI");
        dictionary.Add(04153, "ARAKANDANALLUR");
        // You can look up the int in the dictionary.
        if (dictionary.ContainsKey(04634))
        {
            String value = dictionary[04634];
            Console.WriteLine(value);
        }
    }
}

Output: 输出:

AMBASAMUDRAM

Using LINQ: 使用LINQ:

Dictionary<int, string> dictionary = new Dictionary<int, string>();
    dictionary.Add(04634, "AMBASAMUDRAM");
    dictionary.Add(04253, "ANAMALI");
    dictionary.Add(04153, "ARAKANDANALLUR");

var value = dictionary.FirstOrDefault(x=>x.Key.Contains(04634)).Value; 
Console.WriteLine(value);

Output: 输出:

AMBASAMUDRAM

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我单击 C# Windows 表单中的按钮时,如何从来自文本框的另一个 class 中获取值? - how can I reach the value from another class coming from textbox when I click the button in C# Windows form? 从Button更新文本框,单击C# - Updating textbox from Button click C# 单击按钮时增加文本框值 WPF C# - Increment textbox value on button click WPF C# CodeGo.net&gt; XAML:如何通过单击文本框和按钮打开菜单? - c# - XAML: how to open an menu with textbox and button by button click? 如何使用c#在标签或文本框中显示此linq查询? - how to show this linq query in a label or textbox using c#? 在多行文本框中选择“文本”,单击按钮将其移动到另一个多行文本框中,单击C# - Select Text in a multiline textbox, move it to another multiline textbox on a button click in C# Linq查询以在文本框中显示XML中的字符串 - Linq Query to display string from XML in a TextBox 在ASP.NET C#中,将值从“中继器”行传递到按钮上单击的文本框 - Pass value from a Repeater row to a textbox on button click in asp.net C# C#查询从gridview双击事件更新文本框值 - C# query to update textbox value from gridview double click event 如何在C#中自动对按钮单击进行文本框验证 - How to cause textbox validation on button click automatically in c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM