Hi anjali600,
I have verified your code. There is a mistake in your LINQ syntax.
You need to use e for the employeeId also as you used for EmployeeNumber. Here, e is the generic variable name which represents an entity within a context.
anjali600 says:
e => e.EmployeeNumber.TrimStart('E') == employeeId.TrimStart('E')
This is the correct LINQ syntax.
_akContext.EmployeeInfos.Where(e => e.EmployeeNumber.TrimStart('E') == e.employeeId.TrimStart('E')).OrderBy(e => e.EmployeeInfoId).Select(e => e.EmployeeInfoId).ToList();
Here is the working sample. You can also download the sample zip.
Database
I have made use of the following table EmployeeInfos with the schema as follows.
I have already inserted few records.
Download SQl file
Model
The Model class consists of following properties.
public class EmployeeModel
{
[Key]
public string EmployeeNumber { get; set; }
public string employeeId { get; set; }
public string EmployeeInfoId { get; set; }
}
Database Context
Once the Entity Framework is configured and connected to the database table, the Database Context will look as shown below.
Note: For beginners in ASP.Net Core and Entity Framework, please refer my article ASP.Net Core: Simple Entity Framework Tutorial with example. It covers all the information needed for connecting and configuring Entity Framework with ASP.Net Core.
using Microsoft.EntityFrameworkCore;
namespace Sample_167951
{
public class DBCtx : DbContext
{
public DBCtx(DbContextOptions<DBCtx> options) : base(options)
{
}
public DbSet<EmployeeModel> EmployeeInfos { get; set; }
}
}
Namespaces
using System.Linq;
Controller
public class HomeController : Controller
{
private DBCtx _akContext { get; set; }
public HomeController(DBCtx _context)
{
this._akContext = _context;
}
public IActionResult Index()
{
List<string> employeeInfoId = this._akContext.EmployeeInfos.Where(e => e.EmployeeNumber.TrimStart('E') == e.employeeId.TrimStart('E')).OrderBy(e => e.EmployeeInfoId).Select(e => e.EmployeeInfoId).ToList();
return View();
}
}
View
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
</body>
</html>
Screenshot