0

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

以下是我得到的错误。

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

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; }
}

请指教。

4

1 回答 1

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.
    };

然后,您可以从以下集合创建您的 observable 集合AccountSummary

list = new ObservableCollection(accountSummaries);
于 2013-05-04T09:31:18.613 回答