简体   繁体   English

将列表转换为字典C#

[英]Convert A List To Dictionary C#

I've implemented a search option using Linq that works fine as follows: 我已经使用Linq实现了一个搜索选项,它的工作原理如下:

string[] str = txtSearch.Text.Replace(" ", "").Split(',');

var con = (from c in context.Customer
           join d in context.CustomerType on c.CustType equals d.ID
           where str.Any(t => c.CustName.Contains(t))
           select new { c.CustomerID, c.CustName, c.CustAddress, d.Type }).ToList();

grdDetails.DataSource = con;
grdDetails.DataBind();

But I heard about Dictionary that works very well for lookup rather than List . 但是我听说过Dictionary可以很好地进行查找,而不是List So I've tried to do the following but doesn't get any data to show: 因此,我尝试执行以下操作,但未显示任何数据:

Edited: I've edited the following for Dictionary but it seems like if I work with EF , I have to loop through the List to get with Dictionary . 编辑:我已经为Dictionary编辑了以下内容,但是好像我使用EF一样 ,我必须遍历List才能使用Dictionary By the way, there are no relation between the keys in both dictionaries. 顺便说一句,两个字典中的键之间没有关系。 So I guess, there would be no lookup though tried in another way. 因此,我想,虽然尝试了另一种方法,但不会进行查找。 It works but want to know if it's a good practice. 它可以工作,但是想知道这是否是一个好习惯。

var dictionary = new Dictionary<int, Customer>();
dictionary.Add(1, new Customer() { CustomerID = 1, CustName = "AT", CustType = 1 });
dictionary.Add(2, new Customer() { CustomerID = 2, CustName = "AT-2017", CustType = 1 });
dictionary.Add(3, new Customer() { CustomerID = 3, CustName = "Jackson", CustType = 1 });
dictionary.Add(4, new Customer() { CustomerID = 4, CustName = "Anderson", CustType = 1 });

var dictionary2 = new Dictionary<int, CustomerType>();
dictionary2.Add(1, new CustomerType() { ID = 1, Type = "Active" });

//dictionary.Keys.Where(key => key.Contains("AT")).ToList();

var con = (from c in dictionary
           join d in dictionary2 on c.Value.CustType equals d.Value.ID 
           where str.Any(t => c.Value.CustName.Contains(t))
           select new { c.Value.CustomerID, c.Value.CustName, d.Value.Type }).ToList();

grdDetails.DataSource = con;
grdDetails.DataBind();

I was trying to follow a tutorial that converts a Dictionary into List at the end. 我试图遵循的教程最后将Dictionary转换为List But I am not sure about it why it has done so. 但是我不确定为什么这样做。 So I would like to know if the above way is the perfect one to do the search using Dictionary . 所以我想知道以上方法是否是使用Dictionary进行搜索的理想方法。

Note: I've used Dictionary in the console application and I understood how it works. 注意:我已经在控制台应用程序中使用过Dictionary ,并且我理解了它是如何工作的。 But little bit confused to do the joining using Dictionary . 但是对于使用Dictionary进行加入却有些困惑。

The output of your query is still a list so I'm surprised the con.Values compiles. 您的查询输出仍然是一个列表,因此con.Values会被编译感到惊讶。

Also, the second code example is running a query on two initialized but not populated dictionaries, so no data is a correct result. 另外,第二个代码示例对两个初始化但未填充的字典运行查询,因此没有数据是正确的结果。

I suspect your first code example is better if it is going against the database. 我怀疑您的第一个代码示例如果针对数据库,则更好。 It will use the database's optimization to run the query and return the results. 它将使用数据库的优化来运行查询并返回结果。 There really wouldn't be any advantage of loading all that into memory first then running a query. 将所有内容首先加载到内存然后运行查询确实没有任何优势。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM