How do I tie the values of two columns in a dropdown with distinct commas? (,)

asp.net c# entity-framework

Question

I have a asp.net application in that I am using ado.net entity framework in which I want to bind the two column in that dropdown.for example:

in database their two column of First_name ,Last_name .I want these two columns value come in a single dropdown using C#.

How to do that?

public void BindClients()
{
    //To Bind the Client Names for Searching Option
    var ddlclientnames = (from ddl in mortgageentity.Clients select ddl).ToList();
    if (ddlclientnames.Count() > 0)
    {
        ddlsearchclient.DataSource = ddlclientnames;
        ddlsearchclient.DataValueField = "Client_ID";
        ddlsearchclient.DataTextField = "LastName";
        ddlsearchclient.DataBind();
    }
}
1
4
9/5/2017 2:16:13 PM

Accepted Answer

Unless you use the Fullname a lot, I would suggest to use an anonymous type in your select.

This also limits the amount of data that will be done in your select and other overhead.

var ddlclientnames = (from ddl in mortgageentity.Clients 
                select new { id = ..., FullName = FirstName + Lastname}.ToList();
0
6/23/2011 3:08:36 PM

Popular Answer

What you can do is define a custom property on the Object that does this for you:

Edited for clarity with your objects

You would write something like

public partial class Clients
{
    public string FullName
    {
       get { return String.Format("{0}, {1}", LastName, FirstName); }
    }

}

This will give you a read only property FullName on the Clients Entity.

Then you can do the following

public void BindClients()
{
    //To Bind the Client Names for Searching Option
    var ddlclientnames = (from ddl in mortgageentity.Clients select ddl).ToList();
    if (ddlclientnames.Any)
    {
        ddlsearchclient.DataSource = ddlclientnames;
        ddlsearchclient.DataValueField = "Client_ID";
        ddlsearchclient.DataTextField = "FullName";
        ddlsearchclient.DataBind();
    }

}

I'd also suggest the use of the Any method instead of ddlclientname.Count > 0 as it doesn't require enumerating the entire collection.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow