c# - cannot implicitlyconvert type<SubMenu> to SubMenu -


i'm trying select records contenttypeid

this code i'm trying use

public actionresult loaddata(int contenttypeid)     {         list<productcontent> productcontentlist = (from pc in db.productcontents                                                    pc.contenttypeid == contenttypeid                                                    select pc).tolist();         viewbag.productcontents = productcontentlist;           submenu submenu = (from sm in db.submenulist                                  sm.contenttypeid == contenttypeid                                  select sm);          submenuitem submenuitemlist = (from smi in db.submenuitems                                        smi.contenttypeid == contenttypeid                                        select smi); 

assuming db.submenulist collection of submenu, when this:

from sm in db.submenulist      sm.contenttypeid == contenttypeid      select sm 

you not selecting single record (even if there 1 submenu contenttypeid == contenttypeid), selecting collection of items match predicate. might collection single item. might can ever collection single item, compiler doesn't know , isn't going assume it.

if there ever 1 match like:

    submenu submenu = (from sm in db.submenulist                              sm.contenttypeid == contenttypeid                              select sm).first(); 

should work. or, if item might missing then:

    submenu submenu = (from sm in db.submenulist                              sm.contenttypeid == contenttypeid                              select sm).firstordefault(); 

which set submenu null if no items match condition.


Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -