无法将类型'System.Linq.IQueryable <anonymoustype#1>'隐式转换为'System.Collections.Generic.List </anonymoustype#1>

时间:2013-05-04 09:28:37

标签: wpf linq

我是.NET世界的新手,我试图从LINQ获得一些结果。请帮忙。

以下是我得到的错误。

错误4无法将类型'System.Linq.IQueryable'隐式转换为'System.Collections.Generic.List'。存在显式转换(您是否缺少演员表?).. Business \ DAL \ GlobalRequest.cs 39 27商务

public ObservableCollection<AccountSummary> GetAccountSummary()
    {
        ObservableCollection<AccountSummary> list;
        list = from ListData in
                          (from a in dbc.Accounts
                           join b in dbc.Ledgers on a.Account_ID equals b.Account_ID into t2
                           from t2Data in t2.DefaultIfEmpty()
                           where a.Is_Mannual == Convert.ToChar("Y")

                           select new
                           {
                               Account_ID = a.Account_ID,
                               Account_Name = a.Account_Name,
                               Amount = t2Data.Amount == null ? 0 : t2Data.Amount
                           }
                        ).OrderBy(item => item.Account_Name)
                      group ListData by new
                      {
                          ListData.Account_ID,
                          ListData.Account_Name
                      } into GroupData
                      select new
                      {
                          Account_ID = GroupData.Key.Account_ID,
                          Account_Name = GroupData.Key.Account_Name,
                          Amount = GroupData.Sum(OL => OL.Amount)
                      };

    }

class AccountSummary
{
    public decimal AccountID { get; set; }
    public string AccountName { get; set; }
    public decimal Amount { get; set; }
}

请建议。

1 个答案:

答案 0 :(得分:1)

您的查询将投射到匿名类型的集合(序列):

select new
    {
        Account_ID = GroupData.Key.Account_ID,
        Account_Name = GroupData.Key.Account_Name,
        Amount = GroupData.Sum(OL => OL.Amount)
    };

您需要投射到AccountSummary的集合,例如:

var accountSummaries = 

  ...rest of the query here

select new AccountSummary
    {
        AccountId = ...,
        AccountName = ...,
        etc.
    };

然后,您可以从此AccountSummary

的集合中创建可观察的集合
list = new ObservableCollection(accountSummaries);