Sorangwala Abbasali Sorangwala Abbasali - 1 year ago 74
C# Question

FirstOrDefault does not give appropriate answer

I tried fetching data from my table with entity framework with the help of

The controller code is as follows:

public ActionResult getUser(MembershipModel model)
ProviderImplementation.Edmx.ProviderEntities providerEntities = new ProviderImplementation.Edmx.ProviderEntities();
//var membershipModel = new MembershipModel();
//var model = ProviderImplementation.Models.MembershipModel;
//bool isUserOnline = true;
if (!ModelState.IsValid)
var isValid = Membership.GetUser(model.UserName, true);
if (isValid != null)
var rowData = providerEntities.Users.Where(u => u.UserName.Equals(model.UserName)).FirstOrDefault();
model.UserName = rowData.UserName;
model.Password = rowData.Password;
model.UserId = Convert.ToInt32(rowData.UserId);
model.RoleId = Convert.ToInt32(rowData.RoleId);
//var singleValue = providerEntities.Users.Find(model.UserName);
ViewBag.User = "Found";
ViewBag.User = "Not Found";
var error = ModelState.Values.SelectMany(v => v.Errors);
ModelState.AddModelError("NoModelError", error.ToString());
return View(model);

Also my table data are as follows:
Table Name: User

Table containing Table data and columns

Now I tried so many things it prints well the data in view with the specific record selected.

The problem is here:

{Works fine}--> 1. When I fetch the record with username "abbas" as it gets encountered first with UserId=2 it gets printed well in the view
View code is:

<div style="margin-left: 200px;">
<table cellpadding="10" cellspacing="10">
<th>Get User Details</th>
<td>@Html.LabelFor(m => m.UserId)</td>
<td>@Html.LabelFor(m => m.UserName)</td>
<td>@Html.LabelFor(m => m.Password)</td>
<td>@Html.LabelFor(m => m.RoleId)</td>

{Problem}--> 2. When I fetch the record with username "Abbas" having UserId=4 then FirstOrDefault prints the above data of UserName="abbas" as encountered first and does not print UserName="Abbas" in View.

SingleOrDefault returns an exception telling that it found more than one instance.

Update:While debugging when I change the values it prints perfectly from the database.

Rob Rob
Answer Source

I agree with the previous answers, you should not have a system that differentiates between usernames based on case sensitivity. This is a sign that your structure is poorly designed, so I would suggest altering your system. I can see for example you have roleid in the same table as the user table, if you are generating multiple logins on this table with different case sensitivity as a means to cope with different roles for the same person, there are far better ways to handle this which will cause you much less grief in the long run.

However, if you would like to check for case sensitivity then try:

var rowData = providerEntities.Users.FirstOrDefault(u => u.UserName == model.UserName && String.Compare(u.UserName, model.UserName,false) == 0);

You can also try the following (DB query):

var rowData = db.Users.ToList().Where(x => (string.Compare(x.UserName, model.UserName, false) == 0)).FirstOrDefault();

This assumes your User class is 'Users'.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download