开发者

how to set display member to a concatenated expression? datasource is linq query

i'm trying to set the display member of my listbox to a concatenated value from a linq query:

var query =
     from a in db.Customers
     select a;

listBox1.DataSouce = query;

I was hoping to be able to set the display member by doing something like this:

listBox1.DisplayMember = "FirstName" + "LastName";

but obviously that doesn't work.

how do i do this? I guess the tricky part is the开发者_如何转开发 fact that the datasource is a linq query result object. But its indexable so there's gotta be some way.


If you have a Customer class that you either wrote yourself or is genertaed but extendable, then the best method in my opinion is to add a new property to it

namespace SameNameSpaceAsGeneratedCustomerClass {
  public partial class Customer {
    public string FullName { get { return FirstName + " " + LastName; } }
  }
}

If you are unable to do this, then you will need to look at creating a ValueConverter http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.aspx


Figured it out by accident:

var query =
                from a in db.LUT_Employees
                where a.position == "Supervisor" && a.department == "Production"
                select new { a, Names = a.lastName + ", " + a.firstName };

            cboProductionSupervisor.DataSource = query;
            cboProductionSupervisor.DisplayMember = "Names";

basically what I did was create a new field on the fly just like you would in sql. then use that as the display member


AT first i have to tell you that LINQ query wont execute by itself so if you are writing the above query ie

var query =
     from a in db.Customers
     select a;

listBox1.DataSouce = query;

you wont get the desired output.

for that modify the query to

   var query =  (from a in db.Customers select new{Name = a.FirstName + a.LastName, OtherDetails = a.OtherDetails}).ToList();

listBox1.DataSouce = query;
ListBox1.DataBind();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜