c# - How can I combine these linq queries into one? -


After being new to LINQ, I have created some questions and want to add them in one, but I'm not sure How do it Here are these:

  var u = dc.Users.Where (w = & gt; w.UserName == Username) .SingleOrDefault (); Var M = DC. Amberships Where (w = & gt; w.UserId == u.UserId) .SingleOrDefault (); M.PasswordQuestion = Security Question; M.PasswordAnswer = securityAnswer; Dc.SubmitChanges ();  

dc.Users is aspnet_Users table
dc.memberhip aspnet_embership table

In addition, SingleOrDefault and FirstOrDefault ?

What is the difference between?

Not sure that they have a relationship (they should). If they do, the linq-to-sql designer will give you a user property on the membership object (or on the other side). Then you can write something like this:

  var membership = DC Amberships Where (x = & gt; x.User.UserName == Username) .SingleOrDefault ();  

If they do not have relationships, then you can write something like this:

  var membership = (in meters by MCU User is the user equivalent to USISIS, where you select U. Username == Username U). SingleOther Default (); The difference between SingleOrDefault () and FirstOrDefault () is that SingleOrDefault () assumes that there is no one item matching the query. If two items match the query then an exception will be thrown. While you use firstoriflite () and there are two items that match the query, the first will be selected. 


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -