1.什么是Linq
LINQ(Language Integrated Query)语言集成查询是一组用于c#和Visual Basic语言的扩展。它允许编写C#或者Visual Basic代码以查询数据库相同的方式操作内存数据。
2.Linq基础中用数据访问类的情况
public class UsersData
{
DBDataContext con = new DBDataContext();
public List<Users> SelectAll()
{
List<Users> ulist = new List<Users>();
ulist = con.Users.ToList();
return ulist;
}
public bool Insert(Users u)
{
try
{
con.Users.InsertOnSubmit(u);
con.SubmitChanges();
return true;
}
catch { }
return false;
}
3.Linq抛弃数据访问类与实体类的用法
using (DBDataContext con = new DBDataContext())
{
string s = toolStripTextBox1.Text;
//lambda表达式
List<Users> ulist = con.Users.Where(hehe => hehe.NickName.Contains(s)).OrderByDescending(r => r.Ids).ToList();
foreach (Users u in ulist)
{
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if (listView1.SelectedItems.Count == 1)
{
using (DBDataContext con = new DBDataContext())
{
List<Users> ulist = con.Users.Where(r => r.Ids.ToString() == listView1.SelectedItems[0].Text).ToList();
if (ulist.Count > 0)
{
con.Users.DeleteOnSubmit(ulist[0]);
con.SubmitChanges();
shuaxin();
}
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
using (DBDataContext con = new DBDataContext())
{
Users u = con.Users.Where(r => r.Ids.ToString() == IDS).FirstOrDefault();
if (u != null)
{
u.NickName = textBox3.Text;
u.Sex = Convert.ToBoolean(textBox4.Text);
u.Birthday = Convert.ToDateTime(textBox5.Text);
u.NationCode = textBox6.Text;
con.SubmitChanges();
}
}