使用Linq从C#插入数据

时间:2010-12-27 17:13:26

标签: c# linq

我有版本表,其中包含版本号和版本ID。我需要根据版本号获取版本ID。如何以更有效的方式实现这一目标?

        using (PatchSyncDBDataContext patchSyncDB = new PatchSyncDBDataContext())
        {
            int versionID = (from version in patchSyncDB.AppPM_Versions
                             where version.StatusID != 0 &&
                             version.VersionNumber == versionNumber
                             select version.VersionId).First();

            int customerID = (from reqID in patchSyncDB.MasterRepTables
                              where reqID.rep_email == dtLoginUser
                              select reqID.rep_id).First();

            AppPM_Patch AppPM_Patches = new AppPM_Patch()
            {
                PatchId = patchID,
                IncidentId = incidentID,
                VersionId = versionID,
                RequestedUserId = customerID,
                Emails = emailTo,
                StartDateTime = dateTimes,
                PatchStatus = patchStatus,
                StatusID = 1,
                SelectedProjects = selectedProjects
            };
            patchSyncDB.AppPM_Patches.InsertOnSubmit(AppPM_Patches);
            patchSyncDB.SubmitChanges();
        }

2 个答案:

答案 0 :(得分:0)

假设这是你想知道的代码:

int versionID = (from version in patchSyncDB.AppPM_Versions
                 where version.StatusID != 0 &&
                 version.VersionNumber == versionNumber
                 select version.VersionId).First();

我没有看到更好的方法来执行查询,您可以使用.Where().Select()扩展方法稍微更直接的语法,但如果您的查询参数是{{1} }和VersionNumber = parm我没有看到更好的查询,除了可能返回StatusID != 0的存储过程

答案 1 :(得分:0)

查看

private void button1_Click(object sender, EventArgs e)
    {
        DataClasses1DataContext dc = new DataClasses1DataContext();
        List<Table2> lst = new List<Table2>();
        lst = (from x in dc.Table2s select x).ToList();
        dataGridView1.DataSource = lst; 
    }

搜索

private void button2_Click(object sender, EventArgs e)
    {
        DataClasses1DataContext dc = new DataClasses1DataContext();
        List<Table2> lst = new List<Table2>();
        lst = (from x in dc.Table2s where x.Id==1 select x).ToList();
        dataGridView1.DataSource = lst;
    }

插入

private void button3_Click(object sender, EventArgs e)
    {
        DataClasses1DataContext dc = new DataClasses1DataContext();
        List<Table2> lst = new List<Table2>();
       dc.InsertTable2(Convert.ToDecimal(textBox1.Text), textBox2.Text, Convert.ToChar(textBox3.Text));
        dc.SubmitChanges();
        lst = (from x in dc.Table2s select x).ToList();
        dataGridView1.DataSource = lst;
    }

更新

    private void button4_Click(object sender, EventArgs e)
    {
        DataClasses1DataContext dc = new DataClasses1DataContext();
        List<Table2> lst = new List<Table2>();
        dc.UpdateTable2(2, "a", 'F');
        dc.SubmitChanges();
        lst = (from x in dc.Table2s select x).ToList();
        dataGridView1.DataSource = lst;
    }

删除

    private void button5_Click(object sender, EventArgs e)
    {
        DataClasses1DataContext dc = new DataClasses1DataContext();
        List<Table2> lst = new List<Table2>();
        dc.DeleteTable2(2);
        dc.SubmitChanges();
        lst = (from x in dc.Table2s select x).ToList();
        dataGridView1.DataSource = lst;
    }