Linq To Entity 我最近写的一个小玩意,学习用的,如果能看懂,恭喜你,了解了一些些简单的Linq...

Linq to entity 的 多对多、一对多 CRUD操作
LibraryEntity

  1 using  System;
  2 using  System.Collections.Generic;
  3 using  System.Linq;
  4 using  System.Web;
  5 using  System.Web.UI;
  6 using  System.Web.UI.WebControls;
  7 using  System.Data.Objects;
  8 using  LibraryModel;
  9 using  System.Data.SqlClient;
 10 using  System.Data;
 11 using  System.Collections;
 12
 13 public   partial   class  BookEdit : System.Web.UI.Page
 14 ExpandedBlockStart.gifContractedBlock.gif {
 15    LibraryEntities entities = new LibraryEntities();
 16    protected void Page_Load(object sender, EventArgs e)
 17ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 18        if (!string.IsNullOrEmpty(Request["sn"]))
 19ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 20            hidBookSN.Value = Request["sn"];
 21        }

 22        if (hidMethod.Value == "save")
 23ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 24            SaveBookInformation();
 25            hidMethod.Value = "";
 26        }

 27        BindTypeAndAuthor();
 28        if (hidBookSN.Value != "")
 29ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 30            ReadBookInformation();
 31        }

 32    }

 33    private void SaveBookInformation()
 34ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 35        if (hidBookSN.Value != "")
 36ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 37ContractedSubBlock.gifExpandedSubBlockStart.gif            修改#region 修改
 38            Guid booksn = new Guid(hidBookSN.Value);
 39            var book_type = from b_t in entities.Book_TypeCollect where b_t.Book.SN == booksn select b_t;
 40            foreach (var b_t in book_type)
 41ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 42                entities.DeleteObject(b_t);
 43            }

 44            
 45            var book = (from b in entities.BookCollect where b.SN == booksn select b).First();
 46            int SelectedAuthorID = Convert.ToInt32(selectAuthor.Value);
 47
 48            var author = (from author1 in entities.AuthorCollect where author1.ID == SelectedAuthorID select author1).First();
 49
 50            book.Name = txtBookName.Text;
 51            book.Author = author;
 52            book.PublishDateTime = Convert.ToDateTime(txtPublishDateTime.Value);
 53            book.Description = txtBookName.Text;
 54            for (int i = 0; i < selectBookType.Items.Count; i++)
 55ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 56                if (selectBookType.Items[i].Selected)
 57ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 58                    int SelectedBookTypeID = Convert.ToInt32(selectBookType.Items[i].Value);
 59                    var booktype = (from tmpbooktype in entities.BookTypeCollect where tmpbooktype.ID == SelectedBookTypeID select tmpbooktype).First();
 60                    LibraryModel.Book_Type tmpbook_type = new LibraryModel.Book_Type();
 61                    tmpbook_type.Book = book;
 62                    tmpbook_type.Type = booktype;
 63                }

 64
 65            }

 66            #endregion

 67        }

 68        else
 69ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 70ContractedSubBlock.gifExpandedSubBlockStart.gif            新增#region 新增
 71            int AuthorID = Convert.ToInt32(selectAuthor.Value);
 72            Guid booksn = Guid.NewGuid();
 73            hidBookSN.Value = booksn.ToString();
 74            LibraryModel.Book book = new LibraryModel.Book();
 75            var author = (from a in entities.AuthorCollect where a.ID == AuthorID select a).First();
 76            book.Author = author;
 77            book.SN = booksn;
 78            book.Description = txtBookName.Text;
 79            book.PublishDateTime = Convert.ToDateTime(txtPublishDateTime.Value);
 80            book.Name = txtBookName.Text;
 81            foreach (ListItem li in selectBookType.Items)
 82ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 83                if (li.Selected)
 84ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 85                    int TypeID = Convert.ToInt32(li.Value);
 86                    LibraryModel.Book_Type book_type = new LibraryModel.Book_Type();
 87                    book_type.Book = book;
 88                    book_type.Type = (from t in entities.BookTypeCollect where t.ID == TypeID select t).First();
 89                }

 90            }

 91            #endregion

 92        }

 93        entities.SaveChanges();
 94    }

 95    private void BindTypeAndAuthor()
 96ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 97ExpandedSubBlockStart.gifContractedSubBlock.gif        var authors = from author in entities.AuthorCollect select new { author.Name, author.ID };
 98ExpandedSubBlockStart.gifContractedSubBlock.gif        var booktypes = from booktype in entities.BookTypeCollect select new { booktype.ID, booktype.Name };
 99
100        selectAuthor.DataSource = authors;
101        selectAuthor.DataTextField = "Name";
102        selectAuthor.DataValueField = "ID";
103        selectAuthor.DataBind();
104
105        selectBookType.DataSource = booktypes;
106        selectBookType.DataValueField = "ID";
107        selectBookType.DataTextField = "Name";
108        selectBookType.DataBind();
109    }

110    private void ReadBookInformation()
111ExpandedSubBlockStart.gifContractedSubBlock.gif    {
112        Guid guidBookSN = new Guid(hidBookSN.Value);
113        ObjectQuery<LibraryModel.Book> bookList = entities.BookCollect;
114        ObjectQuery<LibraryModel.BookType> booktypeList = entities.BookTypeCollect;
115        ObjectQuery<LibraryModel.Book_Type> book_typeList = entities.Book_TypeCollect;
116        var queryBook = from book in bookList
117                        where book.SN == guidBookSN
118                        orderby book.PublishDateTime descending
119                        select new
120ExpandedSubBlockStart.gifContractedSubBlock.gif                        {
121                            BookName = book.Name,
122                            AuthorID = book.Author.ID,
123                            PublishDateTime = book.PublishDateTime
124                        }
;
125        var queryBookType = from booktype in book_typeList
126                            where booktype.Book.SN == guidBookSN
127                            select new
128ExpandedSubBlockStart.gifContractedSubBlock.gif                                {
129                                    TypeID = booktype.Type.ID
130                                }
;
131
132        foreach (var q in queryBook)
133ExpandedSubBlockStart.gifContractedSubBlock.gif        {
134            txtBookName.Text = q.BookName;
135            txtPublishDateTime.Value = Convert.ToDateTime(q.PublishDateTime).ToString("yyyy-MM-dd");
136            for (int i = 0; i < selectAuthor.Items.Count; i++)
137ExpandedSubBlockStart.gifContractedSubBlock.gif            {
138                if (q.AuthorID.ToString() == selectAuthor.Items[i].Value)
139ExpandedSubBlockStart.gifContractedSubBlock.gif                {
140                    selectAuthor.Items[i].Selected = true;
141                    break;
142                }

143            }

144        }

145        foreach (var q in queryBookType)
146ExpandedSubBlockStart.gifContractedSubBlock.gif        {
147            for (int i = 0; i < selectBookType.Items.Count; i++)
148ExpandedSubBlockStart.gifContractedSubBlock.gif            {
149                if (q.TypeID == Convert.ToInt32(selectBookType.Items[i].Value))
150ExpandedSubBlockStart.gifContractedSubBlock.gif                {
151                    selectBookType.Items[i].Selected = true;
152                }

153            }

154        }

155    }

156}

转载于:https://www.cnblogs.com/karon/archive/2009/09/15/1566669.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值