locked
Cannot convert System.Linq.IQuerable<ModelB> to ModelB RRS feed

  • Question

  • User657329123 posted

    Hi there,

    I have two tables ModelA and MOdelB. Based on the FirstName from ModelA I'm trying to select records from ModelB and I'm getting this error. Here is my code

    IQueryable<ModelB> dd;
    List<ModelB> pList = new List<ModelB>();
    
    foreach (var item in ModelA)
    {
        dd = (from s in db.ModelB
               where s.FirstName.Contains(item.FirstName) || s.FirstName.Contains(item.OtherName)
                select s).Distinct().OrderBy(s => s.FirstName);
        pList.Add(dd);
    }

    ModelA(TableA) has FirstName and OtherName where as ModelB(TableB) has FirstName and I'm trying to get records from TableB.

    Any ideas what is wrong with my LINQ query?

    Joe

    Tuesday, November 20, 2018 6:33 PM

Answers

  • User475983607 posted

    Error message is Cannot convert System.Linq.IQuerable<ModelB> to ModelB. It is not cut off.

    This code returns IQuerable not a ModelB.

        dd = (from s in db.ModelB
               where s.FirstName.Contains(item.FirstName) || s.FirstName.Contains(item.OtherName)
                select s).Distinct().OrderBy(s => s.FirstName);

    Try invoking the query.

    pList.Add(dd.FirstOrDefault());

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 20, 2018 8:44 PM

All replies

  • User1120430333 posted

    You have not posted all of the error message that has been chopped off in the subject lint. The error message needs to be posted in the Body of the post so that all of the error message can be seen.

    Tuesday, November 20, 2018 6:41 PM
  • User657329123 posted

    Error message is Cannot convert System.Linq.IQuerable<ModelB> to ModelB. It is not cut off.

    Tuesday, November 20, 2018 6:48 PM
  • User475983607 posted

    Error message is Cannot convert System.Linq.IQuerable<ModelB> to ModelB. It is not cut off.

    This code returns IQuerable not a ModelB.

        dd = (from s in db.ModelB
               where s.FirstName.Contains(item.FirstName) || s.FirstName.Contains(item.OtherName)
                select s).Distinct().OrderBy(s => s.FirstName);

    Try invoking the query.

    pList.Add(dd.FirstOrDefault());

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 20, 2018 8:44 PM
  • User-474980206 posted

    dd is IQuerable of ModelB, that is when iterated it will return a collection of ModelA. pList.Add() requires a single object instance of ModelA. You probably want:

      pList.AddRange(dd.AsEnumerable());

    of if you thought the query would only return 1 item:

      pList.Add(dd.First());

    Tuesday, November 20, 2018 8:49 PM
  • User317463322 posted

    AS Mgebhard said even you returning one record but it is not one record, it is actually an IQuerable that contains one record.

    Try to use FirstOrDefualt in the query or as <g class="gr_ gr_348 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="348" data-gr-id="348">Mgebhard</g> said.

    Wednesday, November 21, 2018 7:49 AM
  • User1520731567 posted

    Hi joegreen2005,

    I suggest you could use AddRange to add multiple collections to avoid missing data:

    pList.AddRange(dd.ToList());

     But there may be duplicate records,so you need to use Distinct() to delete redundant records:

    pList.Distinct().ToList();

    Best Regards.

    Yuki Tao

    Wednesday, November 21, 2018 8:08 AM
  • User1120430333 posted

    Error message is Cannot convert System.Linq.IQuerable<ModelB> to ModelB. It is not cut off.

    ModelB is a single object definition that cannot be an IQuerable <ModelB>, which is a collection  that is being returned as a result from the query.  The collection can be queried a single object ModelB cannot be queried..

    https://docs.microsoft.com/en-us/dotnet/api/system.linq.iqueryable?view=netframework-4.7.2

    IQuerable <ModelB> modelbs = Linq query  returning IQuerable <ModelB> results.

    Wednesday, November 21, 2018 9:31 AM
  • User657329123 posted

    Thank you Mgebhard, adding FirstOrDefault() worked.

    Wednesday, November 21, 2018 11:52 AM
  • User475983607 posted

    Thank you Mgebhard, adding FirstOrDefault() worked.

    Keep in mind that it only works properly if you expect one result.  If the query returns multiple results then use AddRange() as suggested above. 

    Wednesday, November 21, 2018 12:01 PM