Question : Creating Ienumerable List in Linq C#

Hi,

I have a dundas chart control which doesn't seem to like binding to the anonymous types produced from linq queries.  Therefore I probably need an Ienumerable list to bind to.  Fernando helped me out yesterday with creating a list of exceptions, but I can't work out how to apply a similar method to work here.

Thanks for your help!
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
// THIS CLASS IS DEFINED EARLIER
    //            public class PaymentChartSet
    //{
    //    public string Payee { get; set; }
    //    public decimal Amount { get; set; }
    // }

            var payments = res
                .Where(x => x is DomPayment)
                .Cast()
                .OrderByDescending(x => x.Amount)
                .Select(x => new { x.PayeeName, x.Currency, x.PayeeBankBSB, x.PayeeBankAccountNum, x.Amount });

            // THIS WORKS BUT CREATES AN ANONYMOUS TYPE I CAN'T BIND TO
            //var paychart = from x in payments
              //          select new { x.PayeeName, x.Amount };
            
                                    
            // I WOULD LIKE THIS TO WORK WITH THE .ToList(); Method
            List paychart = from x in payments
                           select new { x.PayeeName, x.Amount };

            // THIS IS WHAT THE QUERY I WAS USING AS A BASE WAS DOING (different objects)
            //List newEmployees = sf.Except(ff).Select(ei => new EmpInfo { EmployeeID = ei.EmployeeID, Name = ei.Name }).ToList();

Answer : Creating Ienumerable List in Linq C#

Hi Nathan08;

The reason why the following :

// I WOULD LIKE THIS TO WORK WITH THE .ToList(); Method
List paychart = from x in payments
                                                         select new { x.PayeeName, x.Amount };

is not working the way you would like is that the select clause is creating an anonymous type when you say select new { ....  You need to tell the select to create if of a type you want. So it should start like this select new PaymentChartSet  { .... Placing the class type after the new keyword will use that type to create the result. See code snippet complete statement, this is as long as you still have the class PaymentChartSet defined.

Fernando
1:
2:
3:
List paychart = (from x in payments
                                  select new PaymentChartSet { Payee = x.PayeeName, 
                                                               Amount = x.Amount }).ToList();
Random Solutions  
 
programming4us programming4us