0

我无法得到我需要的结果请通过这个。

我正在调用 liq 查询,它会给我这样的结果

"99,85,34,20,1,0.5" as key
"5000199,5000185,5000134,5000120,5000101,5000005"

我希望这在拆分它们后就像键和值格式......就像

key            value
99             5000199
85             5000185

在列表中,键应该是 99,值应该是 5000199 第二个键应该是 85,值应该是 5000185

我正在使用的查询是

var pp = (from a in dc.MediaTypeMasters 
         where a.MediaTypeID == 153 && 
               a.ContentTypeForBilling == "valuepack" 
        select new KeyValuePair<String, String>
                               (a.PartialPricePoints, a.ContentTypeID))
               .ToList();

像这样。

4

4 回答 4

4

LINQ的使用Zip方法:

var keys = "99,85,34,20,1,0.5";
var values = "5000199,5000185,5000134,5000120,5000101,5000005";

var results = keys.Split(',').Zip(values.Split(','), (k, v) => 
                        new KeyValuePair<int, int>(int.Parse(k), int.Parse(v)));

然后,您也可以创建Dicrionary<int, int>

var dict = results.ToDictionary(x => x.Key, x => x.Value);
于 2013-09-18T08:58:39.647 回答
1

所以基本上你想要根据它们在两个集合中的索引的键值对?

string[] first = str1.Split(',');
string[] second= str2.Split(',');
IEnumerable<KeyValuePair<string, string>> pairs = first
    .Select((s, i) => new KeyValuePair<string, string>(s, second.ElementAtOrDefault(i)));

请注意,Enumerable.ElementAtOrDefault如果null第二个集合不包含与first.

于 2013-09-18T08:59:29.350 回答
0
    string key = "99,85,34,20,1,0.5";
    string val = "5000199,5000185,5000134,5000120,5000101,5000005";

    var keyArr = key.Split(',');
    var valArr = val.Split(',');
    var dictionary = new Dictionary<string, string>();
    for (int i = 0; i < keyArr.Length; i++)
    {
        dictionary.Add(keyArr[i], valArr[i]);
    }
于 2013-09-18T08:58:41.973 回答
0

让事情变得简单。

利用

var pp = (from a in dc.MediaTypeMasters where a.MediaTypeID == 153 && a.ContentTypeForBilling == "valuepack" a).ToList();

然后你可以做

var dictionary = pp.ToDictionary(p => p.PartialPricePoints);
于 2013-09-18T09:03:19.143 回答