Sliverlight linq中的数组筛选数据库中的数据

时间:2023-03-08 16:05:57

首先 什么是linq呢 ?

LINQ即Language Integrated Query(语言集成查询),LINQ是集成到C#和Visual Basic.NET这些语言中用于提供查询数据能力的一个新特性。

它是.NET框架的扩展,它允许我们以数据库查询的方式查询数据集合。

借助于LINQ技术,我们可以使用一种类似SQL的语法来查询任何形式的数据。

接下来讲讲我工作中用到的

linq中的数组筛选数据库中的数据

public List GetList1(string cardPhone,string[] carNo)
        {
            var q = from r in DB().TAB_TEL_ADVISORY
                    where r.CUST_TEL == cardPhone || carNo.Contains(r.CAR_NO)
                    select new TelAdvisory
                    {
                        ID = r.ID,
                        CustTel = r.CUST_TEL,
                        CustName = r.CUST_NAME,
                        CarNo = r.CAR_NO,
                        AdvisoryContent = r.ADVISORY_CONTENT,
                        AdvisoryDate = r.ADVISORY_DATE,
                        AnswerResult = r.ANSWER_RESULT,
                        Server = r.SERVER,
                    };
            return q.ToList();
        }

LINQ中的增删改都要调用SubmitChanges方法

public bool ResetPassword(UserType type, int userId, string newCryptPassword)
        {
            var db = DB();
            try
            {
                switch (type)
                {
                    case UserType.雇员:
                        {
                            var tab = db.TAB_EMPLOYEE.Single(r => r.ID == userId);
                            tab.LOGIN_PWD = newCryptPassword;
                            break;
                        }
                    case UserType.合作商:
                        {
                            var tab = db.TAB_PARTNER.Single(r => r.ID == userId);
                            tab.LOGIN_PWD = newCryptPassword;
                            break;
                        }
                }
                db.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

db.account.DeleteOnSubmit(account);
db.account.InsertOnSubmit(account);
 db.SubmitChanges();

linq中的搜索条件,传入的参数是空的或者有值的,或者是关键字

query = (from r in db.TAB_PARTNER
               where ((partnerType != -1 && r.partner_type == partnerType) || partnerType == -1)

&& ((searchKey != "" && r.PARTNER_NAME.IndexOf(searchKey) > -1) || searchKey == "")

orderby r.partner_type ascending

linq to sql分页的问题

Skip(100)代表跳过100行后.从第101行开始分页;

take(10)表示多少行分页; pagesize为每页显示的数据条数

q.Skip(PageSize * (CurrentPage - 1)).Take(PageSize).ToList();