生成操作
DefaultIfEmpty、Empty、Range 和 Repeat,Iqueryable 只支持 DefaultIfEmpty 方法。
var blogs = _context.Blogs.Where(b => b.BlogId > 1).DefaultIfEmpty();
var defaultBlog = new Blog { Name = "零度" }; var blogs = _context.Blogs.Where(b => b.BlogId > 1).DefaultIfEmpty(defaultBlog);
相等比较
SequenceEqual
var blogs1 = _context.Blogs.Where(b => b.BlogId > 5); var blogs2 = _context.Blogs.Where(b => b.BlogId < 2); var result = blogs1.SequenceEqual(blogs2);
var blogs1 = _context.Blogs.Where(b => b.BlogId > 5).AsEnumerable(); var blogs2 = _context.Blogs.Where(b => b.BlogId < 2).AsEnumerable(); var result = blogs1.SequenceEqual(blogs2);
串联运算
Concat
var blogs1 = _context.Blogs.Where(b => b.BlogId > 5); var blogs2 = _context.Blogs.Where(b => b.BlogId < 2); var blogs3 = blogs1.Concat(blogs2);
联接运算
Join 和 GroupJoin
var query = _context.Blogs.Join(_context.Posts, b => b.BlogId, p => p.BlogId, (b, p) => new { b.BlogId, BlogTitle = p.Title, p.Content } );
var query = _context.Blogs.GroupJoin(_context.Posts, b => b.BlogId, p => p.BlogId, (b, ps) => new { b.BlogId, Posts=ps } );
数据分组
GroupBy 和 ToLookup
var query = _context.Blogs.Where(b=>b.BlogId>2).GroupBy(b=>b.OwnerId);
var query = _context.Blogs.Where(b=>b.BlogId>2).GroupBy(b=>b.Owner);
var query1 = _context.Blogs.Where(b=>b.BlogId>2).ToLookup(b =>b.OwnerId); var query2 = _context.Blogs.Where(b => b.BlogId > 2).ToLookup(b => b.Owner);
投影运算
Select 与 SelectMany
var query = _context.Blogs.Select(b => new { BlogName = b.Name + "$", b.Url });
var query = _context.Blogs .Select(b => new { b.BlogId, b.Name, Posts = _context.Posts.Where(p =>p.BlogId == b.BlogId) }) .SelectMany(b => b.Posts);
数据分区
Skip、SkipWhile、Take 和 TakeWhile,SkipLast 和 TakeLast
var query = _context.Blogs.Where(b=>b.BlogId>2).Skip(2);
var query = _context.Blogs.Where(b=>b.BlogId>2).Take(2);
var query = _context.Blogs.Where(b => b.BlogId > 2).Skip(10).Take(10);
var query = _context.Blogs.SkipWhile(b => b.BlogId > 2); var query = _context.Blogs.TakeWhile(b => b.BlogId > 2); var query = _context.Blogs.SkipLast(2); var query = _context.Blogs.TakeLast(2);
数据排序
OrderBy、OrderByDescending、ThenBy、ThenByDescending 和 Reverse
var query = _context.Blogs.Where(b => b.BlogId > 2).OrderBy(b => b.OwnerId);
var query = _context.Blogs .Where(b => b.BlogId > 2).OrderBy(b => b.OwnerId).OrderByDescending(b=>b.BlogId); var query = _context.Blogs .Where(b => b.BlogId > 2).OrderBy(b => b.OwnerId).ThenByDescending(b => b.BlogId);
var query = _context.Blogs.Where(b => b.BlogId > 2).Reverse();
var query = _context.Blogs.Where(b => b.BlogId > 2).AsEnumerable().Reverse();
集合运算
Distinct、Except、Intersect 和 Union
var blogs = _context.Blogs.Where(b => b.BlogId > 5); var query = blogs.Distinct();
var blogs1 = _context.Blogs.Where(b => b.BlogId > 5); var blogs2 = _context.Blogs.Where(b => b.BlogId > 2); var query = blogs1.Except(blogs2); var query = blogs1.Intersect(blogs2); var query = blogs1.Union(blogs2);
限定符运算
All、Any 和 Contains
bool query = _context.Blogs.All(b => b.BlogId > 5);
bool query = _context.Blogs.Any(b => b.BlogId > 5);
var names = new string[] { "A", "B", "C" }; bool query = _context.Blogs.Any(b => names.Contains(b.Name));
var blog = new Blog { BlogId = 1 }; bool query = _context.Blogs.Contains(blog);
bool query = _context.Blogs.Where(b => b.BlogId > 2).Contains(blog);
var query = _context.Blogs.Where(b => b.Name.Contains("ABC"));
var query = _context.Blogs.Where(b => b.Name.StartsWith("A"));
var query = _context.Blogs.Where(b => b.Name.EndsWith("ABC"));