第一篇博客,还望各位大神勿喷

小弟在此代码奉上........

借用NorthWind数据库,实现一个商品展示的小功能。上代码:

添加对Linq的引用

 using System.Data.Linq;//添加对Linq的引用
using System.Data.Linq.Mapping;//配置对象和映射关系的命名空间

由于图简单,所以写了很少的字段,继续代码

    [Table(Name = "Products")]
public class Product
{
[Column(Name = "ProductID", DbType = "int identity(1,1) not null", IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert, CanBeNull = true)]
public int ProductID { get; set; }
[Column(Name = "ProductName", DbType = "nvarchar(40) not null")]
public string ProductName { get; set; }
[Column(Name = "CategoryID", DbType = "int", CanBeNull = true)]
public int CategoryID { get; set; }
[Column(Name = "UnitPrice", DbType = "money")]
public decimal? Price { get; set; }
}
     [Table(Name = "Categories")]
public class Categoty
{
[Column(Name = "CategoryID", DbType = "int identity(1,1) not null", IsPrimaryKey = true, IsDbGenerated = true, CanBeNull = true, AutoSync = AutoSync.OnInsert)]
public int ID { get; set; }
[Column(Name = "CategoryName", DbType = "nvarchar(15) not null")]
public string CategoryName { get; set; }
[Column(Name = "Description", DbType = "ntext null")]
public string Description { get; set; } private EntitySet<Products> _products;
[Association(Name = "C_P", IsForeignKey = false, OtherKey = "CategoryID", ThisKey = "ID", Storage = "_products")]
public IList<Products> Products
{
get { return _products; } //上级接受下级参数,直接返回,上级:Category 下级:Product
set { _products.Assign(value); }//下级接受上级参数,使用Assign方法进行向上转换
}
}

上面分别是两个类:商品类Product与商品类别Category,下面继续创建上下文对象

  public class NorthwindDBContext : DataContext
{
private static readonly string conn = ConfigurationManager.ConnectionStrings["Myconn"].ConnectionString;//数据库的连接 public NorthwindDBContext()
: base(conn) { } public NorthwindDBContext(string connection)
: base(connection) { } public Table<Categoty> Category; public Table<Products> Products;
}

接下来就是展示页面了,创建一个aspx页面吧,扔两个控件上去

 <body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownListCategory" runat="server">
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</div>
</form>
</body>

页面现在也有了,后台代码:

 public partial class ProductsList : System.Web.UI.Page
{
Model.NorthwindDBContext db = new Model.NorthwindDBContext();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
var query = db.Products.Count();
BandCategory();
}
} private void BandCategory()
{ var allCate = from c in db.Catrgorys select c;
this.DropDownListCategory.DataValueField = "ID";
this.DropDownListCategory.DataTextField = "Name";
this.DropDownListCategory.DataSource = allCate;
this.DropDownListCategory.DataBind();
} protected void Button1_Click(object sender, EventArgs e)
{
BandProduct();
} private void BandProduct()
{ //生成日志文件,方便程序出错查看生成的SQL语句
db.Log = new StreamWriter(Server.MapPath("~/log.txt"), true);
int cid = int.Parse(this.DropDownListCategory.SelectedValue);
List<Model.Product> lst = db.Products.ToList();
var Products = db.Products.Where(x => x.CategoryID == cid);
//拿到类别对象
//var cate = db.Category.Where(x => x.CategoryID == cid).First();
this.GridView1.DataSource = Products;//cate.Product;两种方法都行
this.GridView1.DataBind();
db.Log.Flush();
db.Log.Close(); }

最后效果:

表示刚工作学生路过,大神勿喷...以后会陆续分享一些工作心得的

回顾:Linq To SQL语法 - 实体类的更多相关文章

  1. LINQ to SQL 建立实体类

    使用LINQ to SQL时,需要首先建立用于映射数据库对象的模型,也就是实体类.在运行时,LINQ to SQL 根据LINQ表达式或查询运算符生成SQL语句,发送到数据库进行操作.数据库返回后,L ...

  2. 步步学LINQ to SQL:为实体类添加关系【转】

    [IT168 专稿]本文详细为你阐述了如何在你的应用程序中实现LINQ to SQL.附件的示例程序包括了这里探讨的所有代码,还提供了一个简单的WPF图形界面程序来显示通过数据绑定返回的结果集. 第一 ...

  3. LINQ to SQL 建立实体类 (转)

    http://www.cnblogs.com/DebugLZQ/archive/2012/11/14/2770449.html 使用LINQ to SQL时,需要首先建立用于映射数据库对象的模型,也就 ...

  4. Linq to SQL 语法查询(链接查询,子查询 & in操作 & join,分组统计等)

    Linq to SQL 语法查询(链接查询,子查询 & in操作 & join,分组统计等) 子查询 描述:查询订单数超过5的顾客信息 查询句法: var 子查询 = from c i ...

  5. FreeSql (二十四)Linq To Sql 语法使用介绍

    原本不支持 IQueryable 主要出于使用习惯的考虑,如果继承 IQueryable,编写代码的智能总会提示出现一堆你不想使用的方法(对不起,我有强迫症),IQueryable 自身提供了一堆没法 ...

  6. [转]LINQ To SQL 语法及实例大全

    转载自:http://blog.csdn.net/pan_junbiao/article/details/7015633 LINQ to SQL语句(1)之Where Where操作 适用场景:实现过 ...

  7. .net 根据匿名类生成实体类,根据datatable生成实体类,根据sql生成实体类

    在开发中可能会遇到这几种情况 1.EF或LINQ查询出来的匿名对象在其它地方调用不方便,又懒的手动建实体类 2.通过datatable反射实体需要先建一个类 ,头痛 3.通过SQL语句返回的实体也需要 ...

  8. LINQ To SQL 语法及实例大全

    http://blog.csdn.net/pan_junbiao/article/details/7015633 http://blog.csdn.net/pan_junbiao/article/de ...

  9. Linq to sql语法

    LINQ to SQL语句(1)之Where Where操作 适用场景:实现过滤,查询等功能. 说明:与SQL命令中的Where作用相似,都是起到范围限定也就是过滤作用的,而判断条件就是它后面所接的子 ...

随机推荐

  1. Phpcms V9全站伪静态设置方法

    为什么要伪静态?具体在这里就不说了,你懂的!一方面更新修改后不需要生成静态文件,另一方面为了SEO! 访问规则如下 1 2 list-{$catid}-{$page}.html content-{$c ...

  2. PHP安全设置(转载)

    大家都知道PHP已经是当前最流行的Web应用编程语言了.但是也与其他脚本语言一样,PHP也有几个很危险的安全漏洞.所以在这篇教学文章中,我们将大致看看几个实用的技巧来让你避免一些常见的PHP安全问题. ...

  3. liunx运维面试题汇总二

    一.填空题:1. 在Linux系统中,以 文件 方式访问设备 .2. Linux内核引导时,从文件 /etc/fstab 中读取要加载的文件系统. 3. Linux文件系统中每个文件用 i节点 来标识 ...

  4. Python3 如何优雅地使用正则表达式(详解七)

    常见问题 正则表达式是一个非常强大的工具,但在有些时候它并不能直观地按照你的意愿来运行.本篇我们将指出一些最常见的错误. 使用字符串方法 有时使用 re 模块是个错误!如果你匹配一个固定的字符串或者单 ...

  5. C# 文件读写异常“正由另一进程使用,因此该进程无法访问该文件”

    最近在对文件进行读写操作时,利用using的方法可还是遇到了异常"文件正由另一进程使用,因此该进程无法访问该文件": public bool WriteUserInfo(strin ...

  6. java反射 -Class类

    Class类:任何类都是Class类的对象 Class类的实例对象的三种表现形式:1.通过某个类的.class实现 2.某个类的对象的getClass()方法 3.Class.forName() 注意 ...

  7. [POJ] 1948 Triangular Pastures (DP)

    题目地址:http://poj.org/problem?id=1948 题目大意: 给N条边,把这些边组成一个三角形,问面积最大是多少?必须把所有边都用上. 解题思路: 根据题意周长c已知,求组合三边 ...

  8. 转:全志A20 GPIO 总结文档

    链接: http://blog.csdn.net/chwenj/article/details/42190745 /* * author:          chwenj@gmail.com. * A ...

  9. Ubuntu 下升级git到最新版

    $ sudo add-apt-repository ppa:git-core/ppa $ sudo apt-get update $ sudo apt-get install git

  10. 介绍PS大局观很不错的转文

    http://blog.chinaunix.net/uid-20535506-id-1931615.html PowerShell初探 PowerShell的一些特点: ü         内含上百种 ...