さて、私はこのように見えるものを持っています:
会社名-1234
この場合、ダッシュの後に数字を付ける必要がないため、ユーザーが選択できるように名前をコンボボックスに含めるだけにします。
これが、Linqを使用してデータベースにクエリを実行し、ダッシュの後のすべてを削除するコードですが、コンボボックスに何かが入力されているようには見えません。
using (context ctx = new context())
{
List<companyinformation> compInf = new List<companyinformation>();
var getCompanies = (from c in ctx.companyinformations
select c.name).ToList();
foreach (var n in getCompanies)
{
compInf.Add(new companyinformation() { name = Remove(n.LastIndexOf('-')) });
}
cbModClient.DataSource = compInf;
cbModClient.DisplayMember = "name";
cbModClient.SelectedIndex = -1;
};
このコードを試したところ、正しく機能しました。今回は「-」の代わりに「-」を使用したためだと思います。
using (context ctx = new context())
{
List<companyinformation> compInf = new List<companyinformation>(ctx.companyinformations);
var getCompanies = (from c in compInf
where c.name.Contains("-")
select c.name.Substring(0, c.name.LastIndexOf("-"))).ToList();
cbModClient.DataSource = getCompanies;
cbModClient.DisplayMember = "name";
cbModClient.SelectedIndex = -1;
};