I have: 1 Datatable - EmployeeAccess & 1 View - View_HCM

Both have the same column (EmpNo), View_HCM have 1000+ data where EmployeeAccess have only 4 data. View_HCM has email address field and EmployeeAccess has none. I need to get email address from View_HCM according to the 4 data.

public List<EmployeeAccess> EmployeeAccess2()
{
    EmployeeAccess EA = new EmployeeAccess();
    View_HCM VH = new View_HCM();

    var x = from b in contxt.View_HCM
            where b.EmpNo == EA.EmpNo
            select b.EmailAddress;

    return x.ToList();
}

I'm getting this error:

Cannot implicitly convert type 'Systems.Generic.Collections.String<string>' to ... '< list >'
有帮助吗?

解决方案

Your return type is wrong.

To fix the errror change the returned type:

public List<string> EmployeeAccess2()
{
EmployeeAccess EA = new EmployeeAccess();
View_HCM VH = new View_HCM();

var x = from b in contxt.View_HCM
        where b.EmpNo == EA.EmpNo
        select b.EmailAddress;

return x.ToList();
}

And to show the email address in the gridview you can do like this

Define a class with a single named field:

public class EmailRecord
{
public string EmailAddress{ get; set; }
}


public List<EmailRecord> EmployeeAccess2()
{
EmployeeAccess EA = new EmployeeAccess();
View_HCM VH = new View_HCM();

var x = from b in contxt.View_HCM
        where b.EmpNo == EA.EmpNo
        select new EmailRecord
{
    EmailAddress = b.EmailAddress
};

return x.ToList();
}

Bind your grid

gridEmployeeAccess.DataSource = TAClass.EmployeeAccess2(); gridEmployeeAccess.DataBind();

In aspx:

<asp:GridView ID="gridEmployeeAccess" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="EmailAddress" headertext="Email Address"/>
    </Columns>
</asp:GridView>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top