简体   繁体   中英

LINQ Select mutiply columns as data source for combobox C#

I have a combobox that must contain list of clients from(Client table) that own estate(Estate table). Both tables contains ClientID fields. I want to get ClientID from Estate table as a ValueMember and Client Name from Client table as a DisplayMember of combobox like this:

        ClientComboBox.DataSource = ownerQuery;
        ClientComboBox.DisplayMember = "ClientName";
        ClientComboBox.ValueMember = "ClientID";

I have a query that gives me only ClientName:

 var ownerQuery = (from own in AgencyContext.Client
            join clName in AgencyContext.Estate on own.ClientID equals clName.ClientID
            select own.ClientName);

How can I get ClientID and ClientName form one query as source for Combobox.

Change:

var ownerQuery = (from own in AgencyContext.Client
            join clName in AgencyContext.Estate on own.ClientID equals clName.ClientID
            select own.ClientName);

To:

var ownerQuery = (from own in AgencyContext.Client
            join clName in AgencyContext.Estate on own.ClientID equals clName.ClientID
            select own);

With an anonymous type:

 var qry = (from client in AgencyContext.Client
            join estate in AgencyContext.Estate on client.ClientID equals estate.ClientID
            select new 
            {
                ClientId = estate.ClientId,
                ClientName = client.ClientName
            });

which you can use like so:

    ClientComboBox.DataSource = qry;
    ClientComboBox.DisplayMember = qry.First().ClientName;
    ClientComboBox.ValueMember = qry.First().ClientId;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM