• Linq增删改查字段扩展以及一个小练习


    字段扩展:

    因为Linq已经自动生成了一个对应表明的类来封装字段,所以扩展的时候要建立一个名字相同的类并在class后面加上partial进行扩展。扩展的时候this代表这个表,通过this可以.出这个表中的列名以及相关的外键表的表名。

    Linq基本操作:

    首先要创建Linq类,把相关的表拖进VS。

    然后实例化Linq类,通过类名.表名.ToList();来获取所有数据。.AsQueryable();是获得这个方法但是还没有执行。

    Linq高级查询:

    通过类名.表名.where(r=>r.XXX)来进行条件查询。XXX表示点出来的列名,直接在后面.出方法写条件。where换成Count,Max,Min等方法也可以获得相应的结果。

    分页:.Skip().Take()方法,前者是跳过的数据的条数,后者为获取的数据的条数。

    获取交集:A.Intersect(B);A B都是通过不同条件查询出的数据,此方法可以连续调用无数个。

    Linq增删改基本操作:

    增加:首先要创建要添加的对象,然后调用.InsertOnSubmit(对象);方法注册,最后调用.SubmitChanges();方法提交。

    删除:首先要查出来要删除的对象,然后调用.DeleteOnSubmit(对象)方法注册,最后调用.SubmitChanges();方法提交。

    修改:首先要查出来要修改的对象,然后把修改后的数据赋给相应的属性,最后调用.SubmitChanges();方法提交。

    Linq查询分页小练习:

    HTML界面:

     1 <body>
     2     <form id="form1" runat="server">
     3         <table>
     4             <tr>
     5                 <td>学生编号</td>
     6                 <td>学生姓名</td>
     7                 <td>民族</td>
     8                 <td>生日</td>
     9                 <td>科目</td>
    10                 <td>成绩</td>
    11             </tr>
    12             <asp:Repeater ID="Repeater1" runat="server">
    13                 <ItemTemplate>
    14                     <tr>
    15                         <td><%#Eval("Scode") %></td>
    16                         <td><%#Eval("Sname") %></td>
    17                         <td><%#Eval("Snation") %></td>
    18                         <td><%#Eval("Sbirthday","{0:yyyy-MM-dd}") %></td>
    19                         <td><%#Eval("Ssubjectname") %></td>
    20                         <td><%#Eval("Score","{0:#.##}") %></td>
    21                     </tr>
    22                 </ItemTemplate>
    23             </asp:Repeater>
    24         </table>
    25         学生姓名:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
    26         学生民族:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
    27         学生生日:<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="True">
    28             <asp:ListItem>请选择</asp:ListItem>
    29         </asp:DropDownList><asp:DropDownList ID="DropDownList2" runat="server" AppendDataBoundItems="True">
    30             <asp:ListItem>请选择</asp:ListItem>
    31         </asp:DropDownList><asp:DropDownList ID="DropDownList3" runat="server" AppendDataBoundItems="True">
    32             <asp:ListItem>请选择</asp:ListItem>
    33         </asp:DropDownList><br />
    34         所学科目:<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox><br />
    35         考试成绩:<asp:DropDownList ID="DropDownList4" runat="server" AppendDataBoundItems="True">
    36             <asp:ListItem>请选择</asp:ListItem>
    37             <asp:ListItem>&gt;</asp:ListItem>
    38             <asp:ListItem>&lt;</asp:ListItem>
    39             <asp:ListItem>=</asp:ListItem>
    40         </asp:DropDownList><asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br />
    41         <asp:Button ID="Button1" runat="server" Text="查询" /><br />
    42         <asp:LinkButton ID="LinkButton1" runat="server">上一页</asp:LinkButton>
    43         <asp:LinkButton ID="LinkButton2" runat="server">下一页</asp:LinkButton>
    44     </form>
    45 </body>

    扩展字段:

     1 public partial class Linq_Score
     2 {
     3     /// <summary>
     4     /// 显示学号
     5     /// </summary>
     6     public string Scode
     7     {
     8         get { return this.Linq_Student.Student_Code; }
     9     }
    10     /// <summary>
    11     /// 显示名字
    12     /// </summary>
    13     public string Sname
    14     {
    15         get { return this.Linq_Student.Student_Name; }
    16     }
    17     /// <summary>
    18     /// 显示民族
    19     /// </summary>
    20     public string Snation
    21     {
    22         get { return this.Linq_Student.Linq_Nation.Nation_Name; }
    23     }
    24     /// <summary>
    25     /// 显示生日
    26     /// </summary>
    27     public DateTime? Sbirthday
    28     {
    29         get { return this.Linq_Student.Student_Birthday; }
    30     }
    31     /// <summary>
    32     /// 显示课程名称
    33     /// </summary>
    34     public string Ssubjectname
    35     {
    36         get { return this.Linq_Subject.Subject_Name; }
    37     }
    38 }

    C#后台:

      1 ChangYongDataContext CY = new ChangYongDataContext();
      2     protected void Page_Load(object sender, EventArgs e)
      3     {
      4         Button1.Click += Button1_Click;//点击查询
      5         LinkButton1.Click += LinkButton1_Click;//点击上一页
      6         LinkButton2.Click += LinkButton2_Click;//点击下一页
      7         if (!IsPostBack)
      8         {
      9             Session["num"] = 0;
     10             Session["Count"] = 3;
     11             Session["Page"] = 0;
     12             Repeater1.DataSource = Linq().Skip(Convert.ToInt32(Session["Page"]) * Convert.ToInt32(Session["Count"])).Take(Convert.ToInt32(Session["Count"]));
     13             Repeater1.DataBind();
     14             for (int i = 1980; i <= DateTime.Now.Year; i++)
     15             {
     16                 DropDownList1.Items.Add(i.ToString());
     17             }
     18             for (int i = 1; i <= 12; i++)
     19             {
     20                 DropDownList2.Items.Add(i.ToString());
     21             }
     22             for (int i = 1; i <= 31; i++)
     23             {
     24                 DropDownList3.Items.Add(i.ToString());
     25             }
     26             LinkButton1.Enabled = false;
     27         }
     28     }
     29     //下一页
     30     void LinkButton2_Click(object sender, EventArgs e)
     31     {
     32         Session["Page"] = Convert.ToInt32(Session["Page"]) + 1;
     33         Repeater1.DataSource = Linq().Skip(Convert.ToInt32(Session["Page"]) * Convert.ToInt32(Session["Count"])).Take(Convert.ToInt32(Session["Count"]));
     34         Repeater1.DataBind();
     35         if ((Convert.ToInt32(Session["Page"]) + 1) >= Math.Ceiling((Linq().Count / 3.0)))
     36         {
     37             LinkButton2.Enabled = false;
     38         }
     39         LinkButton1.Enabled = true;
     40     }
     41     //上一页
     42     void LinkButton1_Click(object sender, EventArgs e)
     43     {
     44         Session["Page"] = Convert.ToInt32(Session["Page"]) - 1;
     45         Repeater1.DataSource = Linq().Skip(Convert.ToInt32(Session["Page"]) * Convert.ToInt32(Session["Count"])).Take(Convert.ToInt32(Session["Count"]));
     46         Repeater1.DataBind();
     47         if (Convert.ToInt32(Session["Page"]) == 0)
     48         {
     49             LinkButton1.Enabled = false;
     50         }
     51         LinkButton2.Enabled = true;
     52     }
     53     //查询
     54     void Button1_Click(object sender, EventArgs e)
     55     {
     56         Session["Page"] = 0;
     57         Session["num"] = 1;
     58         Repeater1.DataSource = Linq().Skip(Convert.ToInt32(Session["Page"]) * Convert.ToInt32(Session["Count"])).Take(Convert.ToInt32(Session["Count"]));
     59         Repeater1.DataBind();
     60         if ((Convert.ToInt32(Session["Page"]) + 1) >= Math.Ceiling((Linq().Count / 3.0)))
     61         {
     62             LinkButton2.Enabled = false;
     63         }
     64         else
     65         {
     66             LinkButton2.Enabled = true;
     67         }
     68     }
     69     /// <summary>
     70     /// 查询语句
     71     /// </summary>
     72     /// <returns></returns>
     73     public List<Linq_Score> Linq()
     74     {
     75         var list1 = CY.Linq_Score.AsQueryable();
     76         var list2 = CY.Linq_Score.AsQueryable();
     77         var list3 = CY.Linq_Score.AsQueryable();
     78         var list4 = CY.Linq_Score.AsQueryable();
     79         var list5 = CY.Linq_Score.AsQueryable();
     80         if (TextBox1.Text.Trim().Length > 0)
     81         {
     82             list1 = list1.Where(r => r.Linq_Student.Student_Name.Contains(TextBox1.Text.Trim()));
     83         }
     84         if (TextBox2.Text.Trim().Length > 0)
     85         {
     86             list2 = list2.Where(r => r.Linq_Student.Linq_Nation.Nation_Name.Contains(TextBox2.Text.Trim()));
     87         }
     88         if (Session["num"].ToString() == "1" && (DropDownList1.Text != "请选择" || DropDownList2.Text != "请选择" || DropDownList3.Text != "请选择"))
     89         {
     90             list3 = CY.Linq_Score.Where(r => r.Linq_Student.Student_Birthday.Value.Year.ToString() == DropDownList1.Text || r.Linq_Student.Student_Birthday.Value.Month.ToString() == DropDownList2.Text || r.Linq_Student.Student_Birthday.Value.Date.ToString() == DropDownList3.Text);
     91         }
     92         if (TextBox4.Text.Trim().Length > 0)
     93         {
     94             list4 = list4.Where(r => r.Linq_Subject.Subject_Name == TextBox4.Text.Trim());
     95         }
     96         if (TextBox3.Text.Trim().Length > 0 && DropDownList4.Text != "请选择")
     97         {
     98             if (DropDownList4.Text == ">")
     99             {
    100                 list5 = list5.Where(r => r.Score > Convert.ToDecimal(TextBox3.Text));
    101             }
    102             else if (DropDownList4.Text == "<")
    103             {
    104                 list5 = list5.Where(r => r.Score < Convert.ToDecimal(TextBox3.Text));
    105             }
    106             else if (DropDownList4.Text == "=")
    107             {
    108                 list5 = list5.Where(r => r.Score == Convert.ToDecimal(TextBox3.Text));
    109             }
    110         }
    111         var list = list1.Intersect(list2).Intersect(list3).Intersect(list4).Intersect(list5);
    112         return list.ToList();
    113   }
  • 相关阅读:
    海泉湾
    ns2的wirelessphy中添加定时器
    ns2能量模型添加充电模型
    用gawk和gnuplot来分析ns2的trace文件
    [转载]ns2中的能量模型
    配置本地SVN[转载]
    EMQ插件通过HTTP连接认证服务器实现认证
    在K8S上跑一个helloworld
    EMQ插件组合实现物联网边缘平台的设备通信管理
    在CentOS7上搭建Kubernetes
  • 原文地址:https://www.cnblogs.com/mazhijie/p/5759497.html
Copyright © 2020-2023  润新知