I have two List of objects like this:
List<EmployeeInfo> emp = getEmployees();
List<EmailList> emailist = getMasterList();
I want to get all the employees email address, firstname, lastName that exists in emailList, but do not exists in emp list. In other words, I want to exclude the email address of the users that exists in emp list. Below LINQ is working to achieve this:
var result = emailist .Where(p => !emp.Any(p2 => p2.EmailAddress == p.Email))
.Select(o => new {
FirstName = o.FirstName,
LastName = o.LastName,
lastNEmailAddressame = o.Email
}).ToList();
The issue is, var result is generic type list. . I am not sure how to return the generic list of objects from a function. I want to know how to return the generic list from a function and also how can I convert/cast var result to List<employeeInfo> type or List<emailList>. below is my function, I put the return type <T> which is not working.
public List<T> ReminderEmployees()
{
List<EmployeeInfo> emp = getEmployees();
List<EmailList> emailist = getMasterList();
string z = "";
var result = emailist.Where(p => !emp.Any(p2 => p2.Ademail == p.Email))
.Select(o => new
{
FirstName = o.FirstName,
LastName = o.LastName,
lastNEmailAddressame = o.Email
}).ToList();
return result;
}
Below is the model class:
public partial class EmployeeInfo
{
public int EmployeeInfoId { get; set; }
public DateTime DateFormFilled { get; set; }
public string EmployeeNumber { get; set; } = null!;
public string LastName { get; set; } = null!;
public string FirstName { get; set; } = null!;
public string? MiddleName { get; set; }
public string EmailAddress { get; set; } = null!;
}
public partial class EmailList
{
public string? LastName { get; set; }
public string? FirstName { get; set; }
public string? Eid { get; set; }
public string? Email { get; set; }
public DateTime LoadDate { get; set; }
}