entity framework - Where clause with Linq in C# MVC 4 -


i'm trying select field userprofile table, roleid. parameter passed post method username , string textbox working correctly.

    [httppost]     [validateantiforgerytoken]     public actionresult getroles(string username)     {         if (!string.isnullorwhitespace(username))         {              string applyfor = db.profiles                .select(s => s.roleid)                .where(a=>profile.username.contains(username))                .first();              viewbag.applyingfor = applyfor; 

however gives me sequence contains no elements.

i've tried several other methods, such .equals(). i'm pretty sure clause.

what doing wrong here?

note: roleid not part of websecurity, there data in database.

if break down code , highlight each lambda statement returns you'd see issue:

string applyfor = db.profiles                      ^^^^^^^^ 

this returns dbset<profile>.

  .select(s => s.roleid)    ^^^^^^ 

this returns iqueryable<int>. @ point you've lost context of profile , have 0 or more roleids.

so a in statement int value, have no way find username now, , statement literally makes no sense.

  .where(a=>profile.username.contains(username)) 

when rearrange lambda expressions grant winney's answer shows can see why of time select() last thing happens (in simple queries).

i wager there no username on profile. , want

string applyfor = db.profiles                     .where(p => p.user.any(u.username == username))                     .select(p => p.roleid)                     .first(); 

as side note, microsoft best practice camel-case method parameters. recommend method like:

public actionresult getroles(string username) // or username { } 

Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -