KeyPairValue Linq

时间:2016-06-15 15:59:07

标签: c# linq

我想从linq查询中返回一个keypairvalues列表但是当我这样做时我会得到

  

LINQ中仅支持无参数构造函数和初始值设定项   到实体

我认为这是因为KeyValuePair没有空的构造函数但是必须有一种方法可以在linq语句中返回keypairvalue列表,也许可以使用.ToDictionary()或其他东西?

请参阅下面的查询

{
    List<KeyValuePair<string, string>> sup;
    sup = (
        from s in db.LU_opcoVendor
            .GroupBy(x => x.supplierNumber)
            .Select(x => x.FirstOrDefault())
        join su in db.suppliers on s.supplierNumber equals su.supplierNumber
        select new KeyValuePair<string, string>(s.supplierNumber, su.supplierDesc))
        .ToList();
}

1 个答案:

答案 0 :(得分:2)

这是一种方法。您首先转换为内存列表,然后执行转换为List<KeyValuePair<string, string>> sup = (from s in db.LU_opcoVendor.GroupBy(x => x.supplierNumber).Select(x => x.FirstOrDefault()) join su in db.suppliers on s.supplierNumber equals su.supplierNumber select new {s.supplierNumber, su.supplierDesc}) .ToList() // materialize the list back from the data store to memory .Select(s => new KeyValuePair<string, string>(s.supplierNumber, s.supplierDesc)) // now you can use a constructor with parameters .ToList(); // convert the IEnumerable back to a List

SELECT  ID, 
        ActionXml.value('(/*/@*)[2]','int') as ActionID
FROM YourTable