using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Linq
{
public partial class WebForm1 : System.Web.UI.Page
{
#region 頁面加載 + void Page_Load(object sender, EventArgs e)
/// <summary>
/// 頁面加載
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Binding();
}
}
#endregion
#region 查詢 + void Button4_Click(object sender, EventArgs e)
/// <summary>
/// 查詢
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button4_Click(object sender, EventArgs e)
{
using (DataClasses1DataContext cxt = new DataClasses1DataContext())
{
var wh = cxt.Music.Where(a => true);
if (TextBox1.Text != "")//按歌名查找
wh =wh.Where(a => a.MusicName == TextBox1.Text);
if (TextBox2.Text != "")//按歌手查詢
wh = wh.Where(a => a.SingerName == TextBox2.Text);
var list = wh.Select(a => new
{
歌曲 = a.MusicName,
歌手 = a.SingerName
});
if (list.Count() > 0)
{
GridView1.DataSource = list;
GridView1.DataBind();
}
}
}
#endregion
#region 新增 + void Button1_Click(object sender, EventArgs e)
/// <summary>
/// 新增
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text != "" && TextBox2.Text != "")
{
using (DataClasses1DataContext cxt = new DataClasses1DataContext())
{
Music model = new Music();
model.Id = Guid.NewGuid();
model.MusicName = TextBox1.Text;
model.SingerName = TextBox2.Text;
cxt.Music.InsertOnSubmit(model);
cxt.SubmitChanges();
TextBox1.Text = "";
TextBox2.Text = "";
Binding();
}
}
}
#endregion
#region 修改 + void Button2_Click(object sender, EventArgs e)
/// <summary>
/// 修改
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button2_Click(object sender, EventArgs e)
{
if (TextBox1.Text != "" && TextBox2.Text != "")
{
using (DataClasses1DataContext cxt = new DataClasses1DataContext())
{
//List<Music>list = cxt.Music.Where(a => a.MusicName == TextBox1.Text).ToList();
var list = from a in cxt.Music where a.MusicName == TextBox1.Text select a;
if (list != null)
{
foreach (var i in list)
{
i.SingerName = TextBox2.Text;
cxt.SubmitChanges();
}
TextBox1.Text = "";
TextBox2.Text = "";
Binding();
}
}
}
}
#endregion
#region 刪除 + void Button3_Click(object sender, EventArgs e)
/// <summary>
/// 刪除
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button3_Click(object sender, EventArgs e)
{
using (DataClasses1DataContext cxt = new DataClasses1DataContext())
{
// List<Music> list = null;
IQueryable<Music> list=null;
Music model = null;
if (TextBox2.Text != "")//按歌手刪除
// list = cxt.Music.Where(a => a.SingerName == TextBox2.Text).ToList();
list = from a in cxt.Music where a.SingerName == TextBox2.Text select a;
else if (TextBox1.Text != "")//按歌名刪除
//list = cxt.Music.Where(a => a.MusicName == TextBox1.Text).ToList();
list = from a in cxt.Music where a.MusicName == TextBox1.Text select a;
if (list != null)
{
foreach (var i in list)
{
model = i;
cxt.Music.DeleteOnSubmit(model);
cxt.SubmitChanges();
}
TextBox1.Text = "";
TextBox2.Text = "";
Binding();
}
}
}
#endregion
#region 顯示全部數據 - void Binding()
/// <summary>
/// 顯示全部數據
/// </summary>
private void Binding()
{
using (DataClasses1DataContext cxt = new DataClasses1DataContext())
{
//var list = cxt.Music.Where(a => true).Select(a => new
//{
// 歌曲 = a.MusicName,
// 歌手 = a.SingerName
//});
var list = from a in cxt.Music
where true
select new
{
歌曲 = a.MusicName,
歌手 = a.SingerName
};
if (list.Count() > 0)
{
GridView1.DataSource = list;
GridView1.DataBind();
}
}
}
#endregion
}
}
C#Linq小案例
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章
C#線程調用有參和無參函數的方法
佚玥
2020-02-25 23:33:34