简体   繁体   中英

Create a Dictionary<int, List<int>> from a Linq query

I have a table in database as follows...

ID(INT)    Values(VARCHAR(50))  
1          200,300  
2          100  
3          400,500  

when I query, I need to get this data in a Dictionary>

The code I have so far is...

var x = (from o in Values select o).ToDictionary(o=>o.ID, p=>p.Values)

I would like the p.Values to be converted to a List so I also need to perform a Convert.ToInt32() possibly!
Any ideas?

This should work:

var x = 
(from o in Values select o)
.ToDictionary(o => o.ID, 
              p => p.Values.Split(',')
                    .Select(x => Convert.ToInt32(x))
                    .ToList());

.Select(x => Convert.ToInt32(x)) can be converted to a method group like this ;-) .Select(Convert.ToInt32)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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