casting a generic type list to object type

Anjali Agarwal 1,386 Reputation points
2023-05-04T03:12:28.0633333+00:00

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; }
}
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,203 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,287 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 112.5K Reputation points
    2023-05-04T05:16:18.21+00:00

    Check if this is suitable:

    public List<EmailList> ReminderEmployees()
    {
       . . .
       var result = emailist.Where(p => !emp.Any(p2 => p2.EmailAddress == p.Email)).ToList();
       return result;
    }
    

    Or this:

    public List<EmployeeInfo> ReminderEmployees()
    {
       . . .
       var result = emailist.Where(p => !emp.Any(p2 => p2.EmailAddress == p.Email))
          .Select( p => new EmployeeInfo { FirstName = p.FirstName, LastName = p.LastName, EmailAddress = p.Email }).ToList();
       return result;
    }
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful