Reputation: 917
I have a combocox the name is "cmbModel" I want to fill the database with two different values in a table. This is what I have done:
private void Form1_Load(object sender, EventArgs e)
{
using (LINQSQLDataContext db = new LINQSQLDataContext())
{
cmbModel.DisplayMember = "szModel";
cmbModel.DataSource = db.VehEcus.ToList<VehEcu>();
}
}
this will fill my cmbModel with szModel column of my table but I want to avoid repeating , how can I use "distinct
" in query to achieve my goal?
and also I want to show 2 items of my table like "modelID-szModel
" in my combobox
Thanks
Upvotes: 0
Views: 1809
Reputation: 2502
You can apply Distinct() at any point after your query. I recommend doing it before you enumerate.
To create the custom modelID-szModel field, you can enumerate the query using AsEnumerable(), then create an anonymous type and use String.Format to concatenate your values into your new field.
using (LINQSQLDataContext c = new LINQSQLDataContext ())
{
var items = c.VehEcus.Select(t => new
{
a = t.szModel,
b = t.modelID
}).Distinct()
.AsEnumerable().Select(t => new
{
displayMember = String.Format("{0}-{1}", t.a, t.b)
});
cmbModel.DisplayMember = "displayMember";
cmbModel.DataSource = items.ToList();
}
Upvotes: 0
Reputation: 460098
If you just want to show a single column anyway you could select that column and use Distinct
:
cmbModel.DataSource = db.InfoProg_VehEcus.Select(x => x.szModel).Distinct();
Upvotes: 1