0

I have the following Model class:

public partial class EmployeeInfo
{
    public int     EmployeeInfoId   { get; set; }
    public string  FirstName        { get; set; } = null!;
    public int?    JobTitleLookupId { get; set; }

    public virtual JobTitleLookup? JobTitleLookup { get; set; }
}

public partial class JobTitleLookup
{
    public int    JobTitleLookupId { get; set; }
    public string Title            { get; set; } = null!;
    
    public virtual ICollection<EmployeeInfo> EmployeeInfos { get; } = new List<EmployeeInfo>();
}

I have JobTitleLookupId in my employeeeInfo database table. How can i get the name of the jobtitle if i have the corresponding ID in employeeInfo Table.

below is my JobTitleLookup table:

    JobTitleLookupID     title
    1                   title1
    2                   title2
    3                   title3

EmployeeInfo table

    EmployeeInfoId FirstName JobTitleLookupID
    1                test1     2
    2                test3     1
    3                 testx     3

How can I get the job title if I have the employeeinfoId and jobTitleLookupID using LINQ.'

this is what I tried so far:

public async Task<List<string?> GetJobTitle(string employeeId)
{
    var query = _ackContext.EmployeeInfos
        .Include(c => c.JobTitleLookup)
        .Where(c => c.EmployeeNumber == employeeId)
}

this is something I wrote in sql. I want the same in LINQ

SELECT
    title
FROM
    employeeInfo e
    INNER JOIN JobTitleLookup j ON
        e.JobTitleLookupID =j.JobTitleLookupID
        AND
        EmployeeInfoID = 212;
Dai
  • 141,631
  • 28
  • 261
  • 374
rimi
  • 624
  • 5
  • 15
  • Did you try anything? Also if your data is not in-memory, how are you accessing the database? Probably you just want to know how to use [linq-join](https://stackoverflow.com/questions/2767709/join-where-with-linq-and-lambda), in this case this is duplicate. – Lazar Đorđević Feb 10 '23 at 01:23
  • `public string FirstName { get; set; } = null!;` <-- **don't do this argh** – Dai Feb 10 '23 at 04:43
  • what is your question – rimi Feb 10 '23 at 04:45
  • @rimi I don't have a question. _I'm telling you that you are doing something wrong_. – Dai Feb 10 '23 at 04:46
  • @Dai the original poster is clearly using EmployeeInfo as an Entity Framework model, so it's ok to use "null!" to suppress the nullability warning. The FirstName property will be populated by Entity Framework upon the class's instantiation. – user3163495 Feb 10 '23 at 18:48
  • @rimi it is perfectly fine (and actually recommended) to use "null!" for nullable Entity Framework model properties. – user3163495 Feb 10 '23 at 18:49
  • @user3163495 [I strongly disagree on that](https://stackoverflow.com/a/74496499/159145), and doing-so isn't explicitly "recommended" by Microsoft, but I do concede that MS does use `= null!` in their beginner-level tutorial examples for EF Core, but they only use it for `DbSet` members of `DbContext`. There are almost always better ways of _solving_ the problem instead of sweeping legitimate compiler warnings under the rug. (I do have an edit in-the-works to the EF Core documentation that will replace the `= null!` examples with better alternatives but I haven't submitted it as a PR yet). – Dai Feb 10 '23 at 20:48

1 Answers1

0

To select something precise, you have to use Select.

public Task<List<string?> GetJobTitle(string employeeId)
{
    return _ackContext.EmployeeInfos
        .Where(c => c.EmployeeNumber == employeeId)
        .Select(c => c.JobTitleLookup.Title)
        .ToListAsync();
}
Svyatoslav Danyliv
  • 21,911
  • 3
  • 16
  • 32