Click here to Skip to main content
15,074,483 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All,
i am beginner in linq to sql, i want to convert this linq query in List,for that i have created one class that mapped with filed...

C#
/*Class Contain Field return from Linq Query*/
public class CusInfomration
    {
        public string CustomerName
        {
            get;
            set;
        }
        public string CustomerID
        {
            get;
            set;
        }
        public string OrderDate
        {
            get;
            set;
        }
        public string OrderId
        {
            get;
            set;
        }
    }

   var CustomerFromWash =  from p in _NorthWindDataContext.Customers
                           join q in _NorthWindDataContext.Orders 
                           on  p.CustomerID   equals q.CustomerID
                           where p.Region == "WA"
                           select new
                           {                                                   
                             CustomerName =Convert.ToString(p.CompanyName),
                             CustomerID = Convert.ToString(p.CustomerID),
                             OrderId = Convert.ToString(q.OrderID),
                             OrderDate = Convert.ToString(q.OrderDate),
                           };
List<cusinfomration> lstCust = (List<cusinfomration>)CustomerFromWash;

it will give me error Unable to cast object of type 'System.Data.Linq.DataQuery`1[<>f__AnonymousType3`4[System.String,System.String,System.String,System.String]]' to type 'System.Collections.Generic.List`1[LinqToSqlExample.CusInfomration]'


Pls Reply me as as possible
Posted
Updated 10-Sep-11 5:08am
v3

1 solution

That's because a LINQ query is not a List<T>.
use CustomerFromWash.ToList();
   
v2
Comments
jatinchandarana 10-Sep-11 12:06pm
   
List<cusinfomration> lstCust = CustomerFromWash.ToList();
When i write this it throw complie time error.....

Error 1 Cannot implicitly convert type 'System.Collections.Generic.List<anonymoustype#1>' to 'System.Collections.Generic.List<LinqToSqlExample.CusInfomration>'
Simon Bang Terkildsen 10-Sep-11 12:16pm
   
Do you even read the exception messages? It tells you directly that the compiler cannot convert your anonumoustype to CusInfomration. That is you cannot cast
select new
{
CustomerName = Convert.ToString(p.CompanyName),
CustomerID = Convert.ToString(p.CustomerID),
OrderId = Convert.ToString(q.OrderID),
OrderDate = Convert.ToString(q.OrderDate),
};

to CusInfomration

So you need to do that yourself, you could create instances of your type in the LINQ query instead of an anonymous type (new CusInfomration not new)..
jatinchandarana 10-Sep-11 13:43pm
   
Ok,i have solved this....
thanks.....
jatinchandarana 10-Sep-11 13:53pm
   
Solution is...

lstCust = (from p in _NorthWindDataContext.Customers
join q in _NorthWindDataContext.Orders on
p.CustomerID equals q.CustomerID
where p.Region == "WA"
select new CusInfomration
{
CustomerName = Convert.ToString(p.CompanyName),
CustomerID = Convert.ToString(p.CustomerID),
OrderId = Convert.ToString(q.OrderID),
OrderDate = Convert.ToString(q.OrderDate),
}).ToList();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900