将每个单个查询结果作为内容插入到组合框中

时间:2012-03-31 13:38:40

标签: c# .net dynamic combobox

我有这个问题,我期待很多结果。

private void addContentInCmbPhy() {
            DbClassesDataContext myDb = new DbClassesDataContext(dbPath);
            var match = from phy in myDb.Physicians
                                        select phy.Phy_FName;

            for(IQueryable<string> phy in match){
                cmbPhysicians.Items.Add(phy);
            }
        }

在上面的查询中,它会返回几个结果,我希望这些名称结果作为项目插入到我的comboBox中,我该如何添加它?它给了我以下错误

 Error  7   Only assignment, call, increment, decrement, and new object expressions can be used as a statement  C:\Users\John\documents\visual studio 2010\Projects\PatientAdministration\PatientAdministration\Pat_Demog.cs    415 43  PatientAdministration

Error   8   ; expected  C:\Users\John\documents\visual studio 2010\Projects\PatientAdministration\PatientAdministration\Pat_Demog.cs    415 40  PatientAdministration
Error   9   ; expected  C:\Users\John\documents\visual studio 2010\Projects\PatientAdministration\PatientAdministration\Pat_Demog.cs    415 43  PatientAdministration

3 个答案:

答案 0 :(得分:1)

你是不是使用了错误的循环语句?它应该是foreach而不是for。如果你使用for循环,那么你需要一个增量器。

答案 1 :(得分:0)

如果你在datacontext陈述中有using,那就是上帝了。因为只要你需要,你就可以得到连接。你之后直接处理它。我可能会这样做:

private void addContentInCmbPhy() 
{
    List<string> match;
    using (var  myDb = new DbClassesDataContext(dbPath))
    {
        match = (from phy in myDb.Physicians
                select phy.Phy_FName).ToList();
    }


    foreach(var phy in match){
        cmbPhysicians.Items.Add(phy);
    }
}

答案 2 :(得分:0)

private void addContentInCmbPhy() 
{
    List<string> match;
    using (var  myDb = new DbClassesDataContext(dbPath))
{
   cmbPhysicians.Items.AddRange((from phy in myDb.Physicians
            select phy.Phy_FName).ToArray());
}

//  foreach(var phy in match){
//      cmbPhysicians.Items.Add(phy);
//  }
}