我使用此代码选择两个字段
public ViewModelList(CRMEntities crm)
{
var cus = (from c in crm.Customer
select new
{ c.Name, c.Family });
AllCustomer = new ObservableCollection<Custom>(cus.ToList());
}
public ObservableCollection<Custom> AllCustomer
{
get;
set;
}
}
public class Custom
{
public string Name{get;set;}
public string Family { get; set; }
}
,但会出现以下错误
无法从'System.Collections.Generic.List'转换为'System.Collections.Generic.List'
答案 0 :(得分:1)
在ObservableCollection中,您指定该元素的类型为Custom。 执行选择时,您将创建匿名类型。使用新的{...}将创建一个匿名类型。
您可能想要做的是
select new Custom { Name = c.Name, Family = c.Family };