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

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

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

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

 32    }

 33    private void SaveBookInformation()
 34    {
 35        if (hidBookSN.Value != "")
 36        {
 37            修改
 67        }

 68        else
 69        {
 70            新增
 92        }

 93        entities.SaveChanges();
 94    }

 95    private void BindTypeAndAuthor()
 96    {
 97        var authors = from author in entities.AuthorCollect select new { author.Name, author.ID };
 98        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()
111    {
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
120                        {
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
128                                {
129                                    TypeID = booktype.Type.ID
130                                }
;
131
132        foreach (var q in queryBook)
133        {
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++)
137            {
138                if (q.AuthorID.ToString() == selectAuthor.Items[i].Value)
139                {
140                    selectAuthor.Items[i].Selected = true;
141                    break;
142                }

143            }

144        }

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

153            }

154        }

155    }

156}
posted on 2009-09-15 00:16  凯龙  阅读(938)  评论(2编辑  收藏  举报