[Solved] Linq Error: Cannot implicitly convert type System.Collections.Generic.List to System.Collections.Generic.List

ramco1917
 
on Sep 11, 2022 10:53 PM
Sample_726307.zip
1591 Views

Hi

CS0029: Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List<TrainingNominee>'

public List<TrainingNominee> GetSelectedNominees(Int32 TrainingId, Int32 ParticipantId)
{
    try
    {
        List<TrainingNominee> Result = (from t in context.TrainingNominees
                                                  .Where(k => k.TrainingID == TrainingId && k.ParticipantID == ParticipantId)
              join Nominee in context.View_Nominees on t.NomineeId equals Nominee.Id
              select new
              {
                  Name = Nominee.Name
              }).ToList();
              return Result;
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

Thanks

Download FREE API for Word, Excel and PDF in ASP.Net: Download
PrinceG
 
on Sep 12, 2022 03:45 AM

Hi ramco1917,

Instead of returning AnonymousType you need to returned strongly type model object.

Please refer below sample.

HTML

<asp:GridView ID="gvEmployees" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="ID" HeaderText="ID" />
        <asp:BoundField DataField="Name" HeaderText="Name" />
    </Columns>
</asp:GridView>

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        List<Training> trainingNominees = new List<Training>
        {
            new Training { NomineeId=1,  ParticipantId= 101},
            new Training { NomineeId=2, ParticipantId= 111 },
            new Training { NomineeId=3, ParticipantId= 132 }
        };

        List<View_Nominee> viewNominees = new List<View_Nominee>
        {
            new View_Nominee() { Id=1, Name="Nancy" },
            new View_Nominee() { Id=2, Name="Margaret" },
            new View_Nominee() { Id=1, Name="Andrew" }
        };

        List<TrainingNominee> result = (from t in trainingNominees
                                        join n in viewNominees on t.NomineeId equals n.Id
                                        select new TrainingNominee
                                        {
                                            ID = t.NomineeId,
                                            Name = n.Name
                                        }).ToList();
        gvEmployees.DataSource = result;
        gvEmployees.DataBind();
    }
}

public class Training
{
    public int NomineeId { get; set; }
    public int TrainingID { get; set; }
    public int ParticipantId { get; set; }
}

public class View_Nominee
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class TrainingNominee
{
    public int ID{ get; set; }
    public string Name { get; set; }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim trainingNominees As List(Of Training) = New List(Of Training) From {
            New Training With {
                .NomineeId = 1,
                .ParticipantId = 101
            },
            New Training With {
                .NomineeId = 2,
                .ParticipantId = 111
            },
            New Training With {
                .NomineeId = 3,
                .ParticipantId = 132
            }
        }
        Dim viewNominees As List(Of View_Nominee) = New List(Of View_Nominee) From {
            New View_Nominee() With {
                .Id = 1,
                .Name = "Nancy"
            },
            New View_Nominee() With {
                .Id = 2,
                .Name = "Margaret"
            },
            New View_Nominee() With {
                .Id = 1,
                .Name = "Andrew"
            }
        }
        Dim result As List(Of TrainingNominee) = (From t In trainingNominees
                                                  Join n In viewNominees
                                                  On t.NomineeId Equals n.Id
                                                  Select New TrainingNominee With
                                                         {
                                                             .ID = t.NomineeId,
                                                             .Name = n.Name
                                                         }).ToList()
        gvEmployees.DataSource = result
        gvEmployees.DataBind()
    End If
End Sub

Public Class Training
    Public Property NomineeId As Integer
    Public Property TrainingID As Integer
    Public Property ParticipantId As Integer
End Class

Public Class View_Nominee
    Public Property Id As Integer
    Public Property Name As String
End Class

Public Class TrainingNominee
    Public Property ID As Integer
    Public Property Name As String
End Class

Screenshot

PrinceG
 
on Sep 13, 2022 01:27 AM

Hi ramco1917,

Please replace code with below code.

public List<TrainingNominee> GetSelectedNominees(Int32 TrainingId, Int32 ParticipantId)
{
    try
    {
        List<TrainingNominee> Result = (from t in context.TrainingNominees.Where(k => k.TrainingID == TrainingId && k.ParticipantID == ParticipantId)
                                        join Nominee in context.View_Nominees on t.NomineeId equals Nominee.Id
                                        select new TrainingNominee
                                        {
                                            ID = t.NomineeId,
                                            Name = Nominee.Name
                                        }).ToList();
        return Result;
    }
    catch (Exception ex)
    {
        throw ex;
    }
}