FirstOrDefault and other methods are not working in LINQ

C#

using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using practical.Models;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;

namespace practical.Controllers;

public class CustomerController : Controller
{
private readonly ApplicationDbContext _context;
public CustomerController(ApplicationDbContext context)
{
_context = context;
}



public IActionResult Index()
{
return View();
}

public IActionResult CustomerRegister()
{

return View();
}
[HttpPost]
public IActionResult CustomerRegister(customer customer)
{
_context.Add(customer);
_context.SaveChanges();
return RedirectToAction("CustomerLogin");
}
public IActionResult CustomerLogin()
{
return View();

}
[HttpPost]
public IActionResult customerLogin(string Customer_email, string Customer_password)
{
var row = ;
return RedirectToAction("Index");
}

}
C#

using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using practical.Models;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;

namespace practical.Controllers;

public class CustomerController : Controller
{
private readonly ApplicationDbContext _context;
public CustomerController(ApplicationDbContext context)
{
_context = context;
}



public IActionResult Index()
{
return View();
}

public IActionResult CustomerRegister()
{

return View();
}
[HttpPost]
public IActionResult CustomerRegister(customer customer)
{
_context.Add(customer);
_context.SaveChanges();
return RedirectToAction("CustomerLogin");
}
public IActionResult CustomerLogin()
{
return View();

}
[HttpPost]
public IActionResult customerLogin(string Customer_email, string Customer_password)
{
var row = ;
return RedirectToAction("Index");
}

}
41 Replies
Technical Developer
So I am making login signup my signup page is working perfectly but how can write logic of Login page Without Linq methods if they are not working or how to use them why method like FirstOrDefault or Where() are not working in my code
Angius
Angius2w ago
Define "not working"
Technical Developer
! see this is a class task and I am writing code by seeing my old project
C#
[HttpPost]
public IActionResult customerLogin(string Customer_email, string Customer_password)
{
{
var row = _context.tbl_Customer.FirstOrDefault(a => a.Customer_email == Customer_email);
if (row != null && row.Customer_password == Customer_password)
{
HttpContext.Session.SetString("customer_session", row.Customer_id.ToString());
return RedirectToAction("index");
}
else
{
ViewBag.message = "Incorrect Username or Password";
return View();
}

}

}
C#
[HttpPost]
public IActionResult customerLogin(string Customer_email, string Customer_password)
{
{
var row = _context.tbl_Customer.FirstOrDefault(a => a.Customer_email == Customer_email);
if (row != null && row.Customer_password == Customer_password)
{
HttpContext.Session.SetString("customer_session", row.Customer_id.ToString());
return RedirectToAction("index");
}
else
{
ViewBag.message = "Incorrect Username or Password";
return View();
}

}

}
see I want something like that to get login functionality but it is not working
canton7
canton72w ago
What exactly is it doing, which makes you think that it isn't working?
Technical Developer
it highlight its with a red line and also not show me this in intelllisense
canton7
canton72w ago
Right, and what is the error message it shows in the "Error List" pane?
canton7
canton72w ago
("Not working" there could have meant that the code ran but always returned "Incorrect Username or Password" -- that's what I assumed you meant. You need to be precise when talking to other people) Can you paste the definition of ApplicationDbContext? _context.tbl_Customer should not be an instance of ApplicationDbContext
Angius
Angius2w ago
_context should be the ApplicationDbContext here, and tbl_Customer (as bad of a name as it is) should be a DbSet<Customer>
Technical Developer
Sorry I don't have a strong vocabulary like you in english
C#

DbSet<customer> customers { get; set; }
C#

DbSet<customer> customers { get; set; }
see i have declare it like this but it is also not getting in controller part
canton7
canton72w ago
Paste the whole class, please. I want to see what tbl_Customer is
Technical Developer
forget tbl_Customer i was just trying to copy my old project which was similar to this in that project i named tbl_Customer in my new project it is names as customer.cs
C#

using System.ComponentModel.DataAnnotations;

namespace practical.Models
{
public class customer
{
[Key]
public int Id { get; set; }
public string name { get; set; }
public string email { get; set; }
public string password { get; set; }
}
}
C#

using System.ComponentModel.DataAnnotations;

namespace practical.Models
{
public class customer
{
[Key]
public int Id { get; set; }
public string name { get; set; }
public string email { get; set; }
public string password { get; set; }
}
}
and here is my ApplicationDbContext.cs
C#
using Microsoft.EntityFrameworkCore;

namespace practical.Models
{
public class ApplicationDbContext:DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options ) : base (options)
{

}
DbSet<customer> customers { get; set; }


}
}
C#
using Microsoft.EntityFrameworkCore;

namespace practical.Models
{
public class ApplicationDbContext:DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options ) : base (options)
{

}
DbSet<customer> customers { get; set; }


}
}
@canton7
canton7
canton72w ago
I'm confused as to why the compiler didn't throw an error saying that _context.tbl_Customer doesn't exist, then You need _context.customers.FirstOrDefault(x => x.email == ...) of course
Technical Developer
don't mix tbl_Customer
canton7
canton72w ago
What do you mean?
Technical Developer
because I am telling you 2nd time that I want it to create like that this tbl_Customer is my old project having this model classs this is the part of new one
canton7
canton72w ago
I don't understand. Your ApplicationDbContext has DbSet<customer> customers { get; set; } . You pasted that code above. That means you need to use _context.customers
Technical Developer
yes i want to _context.customers table _context.customer.FirstOrDefault(c => c.id = id);
canton7
canton72w ago
Yes. Do that.
Technical Developer
yes but the thing is when i write this line it gives me this error
canton7
canton72w ago
What error?
Technical Developer
see attachment
canton7
canton72w ago
Which attachment? Last time, you showed code which used _context.tbl_Customer, which is wrong
Technical Developer
see
canton7
canton72w ago
Show the code above that error
Technical Developer
can you come on vc so i can share my screeen and
canton7
canton72w ago
No, I'm at work
Technical Developer
betterunderstand ok!
Technical Developer
public IActionResult customerLogin(string Customer_email, string Customer_password)
{
var row = _context.customers.FirstOrDefault();
return RedirectToAction("Login");
}
public IActionResult customerLogin(string Customer_email, string Customer_password)
{
var row = _context.customers.FirstOrDefault();
return RedirectToAction("Login");
}
'
No description
Technical Developer
see
MODiX
MODiX2w ago
Technical Developer
From Technical Developer
React with ❌ to remove this embed.
canton7
canton72w ago
You need public DbSet<customer> customers { get; set; }
Technical Developer
it is now giving me this error
C#
using Microsoft.EntityFrameworkCore;

namespace practical.Models
{
public class ApplicationDbContext:DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options ) : base (options)
{

}
DbSet<customer> customers { get; set; }


}
}
C#
using Microsoft.EntityFrameworkCore;

namespace practical.Models
{
public class ApplicationDbContext:DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options ) : base (options)
{

}
DbSet<customer> customers { get; set; }


}
}
see
canton7
canton72w ago
You need public DbSet<customer> customers { get; set; }
Technical Developer
i have already written here `DbSet<customer> customers {get ; set}
canton7
canton72w ago
You need public DbSet<customer> customers { get; set; }
Technical Developer
this property is already mentioned if you see my dbContext clas
canton7
canton72w ago
Please read what I wrote very carefully public DbSet<customer> customers { get; set; }
Technical Developer
ok when i clean the solution and rebuild after adding public it now finally works thanks

Did you find this page helpful?