English 中文(简体)
在C#中加入使用Linq的数据
原标题:Insert Data using Linq from 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();
        }
问题回答

假设这一点,你会问:

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

我看不出以什么更好的方式来进行询问,你可能会使用<代码>稍加直接的辛迪加。 在()和.Select(>推广方法中,但如果你的查询参数为VersionNumber = parmStatusID ! 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();
        dataGrid观点1.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();
        dataGrid观点1.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();
        dataGrid观点1.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();
        dataGrid观点1.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();
        dataGrid观点1.DataSource = lst;
    }




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签