none
Cannot implicitly convert type 'System.Linq.IQueryable<string>' RRS feed

  • Question

  •  

     

    I am quite new to LINQ and am trying to have what I guess are extensions to allow multiple modifications to the query before it is sent. I jumbled a bunch of samples together to fit with my data but am running into problems and not sure if im going about this the right way.

    My two main problems are

    1) The resulting query is returning just one table although I can access the relations such as db.Products.Category.CategoryName due to the relations set in the designer (perhaps I need a join, but in either case its not working because of problem #2)

    2) The resulting query is returning the every field in the products table, I cannot seem to do p.ProductID, p.ProductName, etc without generating errors.

     

    // This is working but I cannot do do p.ProductID, p.ProductName, etc

    DataClassesDataContext db = new DataClassesDataContext(); var products = from p in db.products select p; products = GetProductFilters(products, db); // Im passing in the db context because im stupid and need to access another table GridView1.DataSource = events; GridView1.DataBind();


    //When trying this the GetProductFilters fails with error
    //cannot convert from 'System.Linq.IQueryable<AnonymousType#1>' to 'System.Linq.IQueryable<Product>'

    var
    products = from p in db.products select new { p.ProductID, p.ProductName };



    I have tried changing "var products =" to "IQueryable<Product> ="..  Im sure this is a simple problem but im stuck

     

    // Here is the method that does the filters
    private IQueryable<Product> GetProductFilters(IQueryable<Product> ProductsQuery, DataClassesDataContext db) { if (!string.IsNullOrEmpty(_SearchTerm)) // _SearchTerm is local class var { ProductsQuery = ProductsQuery.Where(p => p.ProductDescription.Contains(_SearchTerm)); }

    return ProductsQuery;
    }

    I suspect the answer is glaringly simple but im new to LINQ so please help!

     




    • Edited by TimDeveloper Thursday, November 17, 2011 10:30 AM
    Thursday, November 17, 2011 10:26 AM

Answers

  • Hi Tim;

    Not sure what you mean by this comment above the following query because the following query in your question does select individual fields from the database.

    // This is working but I cannot do do p.ProductID, p.ProductName, etcDataClassesDataContext db = new DataClassesDataContext();

    var products = from p in db.products
      select p;


    About this error message, "When trying this the GetProductFilters fails with error cannot convert from 'System.Linq.IQueryable<AnonymousType#1>' to 'System.Linq.IQueryable<Product>'", this is because the function GetProductFilters is wanting to return an entity of type product but you are retuning an anonymous type. the way you know this is that the select clause has the new keyword without a type. When you do that the query builds an anonymous type. If you want to return a Product type back the select clause must be as follows:

    var products = from p in db.products
                   select p;

    To this part of your question, "1) The resulting query is returning just one table although I can access the relations such as db.Products.Category.CategoryName due to the relations set in the designer (perhaps I need a join, but in either case its not working because of problem #2)", if you mean that you want to load all the related Category for each Products then you need to tell Linq to SQL that because it will do lazy loading as you state in your question.
     
    DataClassesDataContext db = new DataClassesDataContext();
    DataLoadOptions dlo = new DataLoadOptions();
    dlo.LoadWith<Products>(p => p.Category);
    db.LoadOptions = dlo;

    Now as you get a Products record it will also return the related Category records.


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Thursday, November 17, 2011 7:52 PM

All replies

  • Hi Tim;

    Not sure what you mean by this comment above the following query because the following query in your question does select individual fields from the database.

    // This is working but I cannot do do p.ProductID, p.ProductName, etcDataClassesDataContext db = new DataClassesDataContext();

    var products = from p in db.products
      select p;


    About this error message, "When trying this the GetProductFilters fails with error cannot convert from 'System.Linq.IQueryable<AnonymousType#1>' to 'System.Linq.IQueryable<Product>'", this is because the function GetProductFilters is wanting to return an entity of type product but you are retuning an anonymous type. the way you know this is that the select clause has the new keyword without a type. When you do that the query builds an anonymous type. If you want to return a Product type back the select clause must be as follows:

    var products = from p in db.products
                   select p;

    To this part of your question, "1) The resulting query is returning just one table although I can access the relations such as db.Products.Category.CategoryName due to the relations set in the designer (perhaps I need a join, but in either case its not working because of problem #2)", if you mean that you want to load all the related Category for each Products then you need to tell Linq to SQL that because it will do lazy loading as you state in your question.
     
    DataClassesDataContext db = new DataClassesDataContext();
    DataLoadOptions dlo = new DataLoadOptions();
    dlo.LoadWith<Products>(p => p.Category);
    db.LoadOptions = dlo;

    Now as you get a Products record it will also return the related Category records.


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Thursday, November 17, 2011 7:52 PM
  • Hi TimDevelper,

    I marked Fernando's reply as answer, if you have any problem about this issue, please feel free to post here.

    @Fernando Soto, thanks for your reply, you really make a great contribution to the forum, expecting your wonderful reply in other cases.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    Tuesday, November 22, 2011 6:15 AM
    Moderator
  •  

    Hi Allen; Will try.  ;=)

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Tuesday, November 22, 2011 2:26 PM