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

2018-06-18 02:51:47来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

首先 什么是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();

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:大话程序猿眼中的聚集索引和非聚集索引

下一篇:各大招聘网站信息实时查询浏览