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

asp.net c# entity-framework

Question

I want to link the two columns in that dropdown in an asp.net application where I'm utilizing the ado.net entity framework. for instance:

First name and Last name are the two columns in the database. Using C#, I want the values of these two columns to appear in a single dropdown.

How do you 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

I would advise using an anonymous type in your pick unless you often utilize the Fullname field.

As a result, less data will be processed for your choose 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

You may do this by creating a custom property on the object that performs this work for you:

clarified with your objects after editing

You may type something like that.

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

}

You'll get a read-only property as a result.FullName on theClients Entity.

then you may do the following action.

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 advise making advantage of theAny method as opposed toddlclientname.Count > 0 because it avoids having to list the complete 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