user2198989
user2198989

Reputation: 13

Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#1>' to 'System.Collections.Generic.List

I am a newbie in .NET world, i was trying to achieve some result from LINQ. Please help.

Following is the error i am getting.

Error 4 Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Collections.Generic.List'. An explicit conversion exists (are you missing a cast?) ..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; }
}

Please advice.

Upvotes: 0

Views: 2162

Answers (1)

devdigital
devdigital

Reputation: 34349

Your query is projecting to a collection (sequence) of anonymous types:

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

You will need to project to a collection of AccountSummary, e.g:

var accountSummaries = 

  ...rest of the query here

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

You can then create your observable collection from this collection of AccountSummary:

list = new ObservableCollection(accountSummaries);

Upvotes: 1

Related Questions