如何将'System.Collections.Generic.List <anonymoustype#1>'转换为IEnumerable </anonymoustype#1>

时间:2011-10-16 05:17:32

标签: wpf

我使用此代码选择两个字段

     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'

1 个答案:

答案 0 :(得分:1)

在ObservableCollection中,您指定该元素的类型为Custom。 执行选择时,您将创建匿名类型。使用新的{...}将创建一个匿名类型。

您可能想要做的是

select new Custom { Name = c.Name, Family = c.Family };
相关问题