EF基本方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using System.Data.Common;

namespace QueryString
{
    class Program
    {
        static void Main(string[] args)
        {
            WanceMISEntities MISData = new WanceMISEntities();
            ObjectQuery intern = MISData.Intern;//第一种访问方式
            ObjectQuery intern1 = new ObjectQuery("select it.InternID,it.InternPlace from WanceMISEntities.Intern as it", MISData);//第二种访问方式
            ObjectQuery intern2 = new ObjectQuery("select value count(it.InternID) from WanceMISEntities.Intern as it ", MISData);
           
            foreach (var item in intern1)
            {
                //Console.WriteLine("{0},{1}",item.InternID,item.InternPlace);
                Console.WriteLine("{0},{1}",item[0].ToString(),item[1].ToString());
            }
            foreach (var item in intern2)
            {
                Console.WriteLine("个数为:{0}",item.ToString());
            }
            bool b = MISData.Intern.All(s => s.InternID == "1");//判断集合中是否所有元素都满足某一条件
            if (b == true)
            {
                Console.WriteLine("正确");
            }
            bool p = MISData.Intern.Any(s => s.InternID == "0");//判断集合中是否有元素满足某一条件
            if (p == true)
            {
                Console.WriteLine("正确");
            }
            else
            {
                Console.WriteLine("错误");
            }
            IQueryable internlist = MISData.Intern.Take(3);//获取集合的前三个元素
            foreach (var item in internlist)
            {
               
                Console.WriteLine("{0},{1}",item.InternID,item.InternPlace);
            }
            IQueryable internlist1 = MISData.Intern.OrderBy(s => s.InternID).Skip(0);//跳过集合的前n个元素 注:Linq To Entity 要求必须先OrderBy
            foreach (var item in internlist1)
            {

                Console.WriteLine("{0},{1}", item.InternID, item.InternPlace);
            }
            Intern internlist2 = MISData.Intern.First();//无选择条件的
            Intern internlist3 = MISData.Intern.First(s=>s.InternID=="1");//有选择条件的
            Console.WriteLine("{0},{1}", internlist2.InternID, internlist2.InternPlace);
            Intern internlist4 = MISData.Intern.FirstOrDefault(s => s.InternID == "1");//集合中的第一个元素,没有则返回类型默认值,对象型默认值为null
            if (internlist4 != null)
            {
                Console.WriteLine("有数据");
            }
            IQueryable internlist5 = MISData.Intern.Where(s => s.InternID == "1");//用LinqExpressions为条件进行查询/GroupBy()来进行分组
            if (internlist5 != null)
            {
                Console.WriteLine("呦吼");
            }
            IQueryable internlist6 = MISData.Intern.Distinct();//过滤集合中的相同项
            IQueryable internlist7 = MISData.Intern.OrderByDescending(s => s.InternID);//排序升,排序降
            IQueryable internlist8 = MISData.Intern.OrderByDescending(s => s.InternID).ThenByDescending(q=>q.InternDesc);//再次排序
            //decimal d =MISData.Intern.Sum(s=>s.InternID);
            //decimal m = MISData.Intern.Average(s=>s.InternID);
            var max = MISData.Intern.Max(s => s.InternID);//获取最大值Min()为最小值
            Console.WriteLine(max);
            int x = MISData.Intern.Count();//计算数目
            Console.WriteLine(x);
            long y = MISData.Intern.LongCount();//长类型的数目
            Console.WriteLine(y);
            IQueryable list1 = MISData.Intern.Where(s => s.InternID == "1");
            IQueryable list2 = MISData.Intern.Where(s => s.InternID == "2");
            IQueryable list = list1.Concat(list2);//或者用Union()/UnionAll()来合并
            IQueryable list0 = list1.Except(list2);//排除,或者用Intersect()方法表示相交的意思/Join()来连接
            var Lquery = MISData.Intern.Select(s => new {a=s.InternID,s.InternPlace});//选择
            //var v = query1.GroupJoin(query2, temp1 => temp1.ItemID, temp2 => temp2.ItemID, (temp1, temp2) => new { temp1.ItemID, temp1.ItemMatter, 个数 = temp2.Count() }); //用GroupJoin连接组合
            //List li =MISData.Intern.ToList();
            //Intern r = list.Aggregate(( x,  y) => new Intern() { ItemValue = x.ItemValue + y.ItemValue });
            //List li1 = MISData.Intern.ToList();

            //IEnumerable v = list.TakeWhile(s => s.InternID >= 2); //TakeWhile或用SkipWhile()
            IEnumerable li2 = MISData.Intern.AsEnumerable();

            IEnumerable v = list.Reverse(); //Reverse()
            Intern li3 = li2.Last();//last()或者为LastOrDefault()同理Single(),SingleOrDefault(),Contains (),Distinct()
            List li4 = MISData.Intern.ToList();
            Intern i1 = li4.ElementAt(0);//第几个元素
            Intern i2 = li4.ElementAtOrDefault(0);//SelectMany ()同理
          
            Intern r = new Intern();
            r.InternID = "1";
            var ww = li4.DefaultIfEmpty(r);
            Console.WriteLine(ww);
            //bool b = list1.SequenceEqual(list2);
             }
 }
}

你可能感兴趣的:(Asp.net编程开发)