LINQ - From LINQ query to list - Asked By Partha Chakraborty on 14-Jul-11 06:18 AM

Hi,

I am trying to write a LINQ query which will retrieve data from Customers table and store selected data in a List<string>.

I have written the following code:

public List<string> FillGrid()

{
        LINQDALDataContext db = new LINQDALDataContext();
        List<string> query =

        from c in db.Customers

        where (c.Country == "USA"

            && c.City == "WA")

        select new { c.CustomerID, c.ContactTitle, c.Country, c,City};

        return query.ToList();

}

I am getting the error in select statement:
Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#1>' to 'System.Collections.Generic.List<BusinessObjects.Customers>'. An explicit conversion exists (are you missing a cast?)

I have tried a lot.

Any help will be thankfully accepted.

Partha

Jitendra Faye replied to Partha Chakraborty on 14-Jul-11 06:34 AM
Yor are getting error because of this line-

 List<string> query =

      from c in db.Customers

      where (c.Country == "USA"

        && c.City == "WA")

      select new { c.CustomerID, c.ContactTitle, c.Country, c,City};

      return query.ToList();

I select statement you are returing more that one field, so that you have to store these file in any class object or var variable.

like this-

var result=

      from c in db.Customers

      where (c.Country == "USA"

        && c.City == "WA")

      select new { c.CustomerID, c.ContactTitle, c.Country, c,City};

      return query.ToList();

Hope this will help you.

Partha Chakraborty replied to Jitendra Faye on 14-Jul-11 06:40 AM
Thanks for the reply.
I have already tried with var. But it can not convert "query.ToList()" statement to List.
What should I do.
Asked By Partha Chakraborty on 14-Jul-11 06:42 AM
I have already tried this but it is not working.
Thanks.
Jitendra Faye replied to Partha Chakraborty on 14-Jul-11 07:01 AM
Check this code -

public List<Customers> FillGrid()

{
      LINQDALDataContext db = new LINQDALDataContext();
      List<Customers> query =

      from c in db.Customers

      where (c.Country == "USA"

        && c.City == "WA")

      select new { c.CustomerID, c.ContactTitle, c.Country, c,City};

      return query.ToList();

}

Try and let me know.

Web Star replied to Partha Chakraborty on 14-Jul-11 07:04 AM

To convert IQuerable or IEnumerable to a list, you can do one of the following:

IQueryable<object> q = ...; 
List<object> l = q.ToList(); 

or:

IQueryable<object> q = ...; 
List<object> l = new List<object>(q); 
Partha Chakraborty replied to Jitendra Faye on 14-Jul-11 07:09 AM
Getting same error in select statement. :(
Jitendra Faye replied to Partha Chakraborty on 14-Jul-11 07:15 AM

Yes, I got it,

Mention the class name after new operator.


like this-

public List<Customers> FillGrid()

{
    LINQDALDataContext db = new LINQDALDataContext();
    List<Customers> query =

    from c in db.Customers

    where (c.Country == "USA"

      && c.City == "WA")

    select new  Customers{ c.CustomerID, c.ContactTitle, c.Country, c,City};

    return query.ToList();

}

Try and let me know.

Partha Chakraborty replied to Jitendra Faye on 14-Jul-11 08:13 AM
Hi Vickey,
I have tried.  I am getting an error in select statement an it is saying "Cannot initialize type 'BusinessObjects.Customers' with a collection initializer because it does not implement 'System.Collections.IEnumerable".
Any clue.