【发布时间】:2015-04-08 23:40:11
【问题描述】:
我正在尝试使 jquery 自动完成,其中我使用标签和值 like in this post,这意味着我需要我的 json 格式为
{ label: 'label text', value: 'value text' }
但是我正在过滤一个员工列表,它是一个具有以下结构的类:
public sealed class Employee
{
public string Name { get; set; }
public string PersonnelNumber { get; set; }
public int RecID { get; set; }
public string Email { get; set; }
}
所以我尝试了以下Linq来获取标签的格式,我需要的值:
var jsonResult = employees
.SelectMany(emp => new { label = emp.Name, value = emp.RecID })
.ToList();
employees 是 Employee 对象的列表,但它引发了构建错误
错误 1 无法从用法中推断方法“System.Linq.Enumerable.SelectMany(System.Collections.Generic.IEnumerable, System.Func>)”的类型参数。尝试明确指定类型参数。
如何解决此问题以在新的对象列表中获取 Name 和 RecID,并将 label 和 value 作为其输出?
【问题讨论】:
-
如果你要回家 - 那我也是! :D