《深入理解c#》第十一章中LINQ查询表达式总结
作者:互联网
《深入理解c#》第十一章介绍了查询表达式的基本语法以及Linq to SQL中各种常见操作。虽然我有一定的SQL基础,但初次接触查询表达式还是难免容易有遗忘、混淆,这里把书中这一章出现过的查询表达式在这里整理一下:
# 11.2.1
var query = from user in SampleData.AllUser select user;
# equals to:
query = SampleData.AllUser.select(user=> user);
# 11.2.3
IEnumerable<string> query = from user in SampleData.AllUser
select user.Name;
# Select扩展方法签名:
static IEnumerable<TResult> Select<TSource, TResult>
(this IEnumerable<TSource> source,
Func<TSource, TResult> selector);
# 11.2.4
ArrayList list = new ArrayList{"First", "Second", "Third"};
var string = from string entry in list
select entry.Substring(0, 3);
#equals to:
list.Cast<string>().Select(entry=>entry.Substring(0, 3));
# 11.3
# 11.3.1
User tim = SampleData.Users.TesterTim;
var query = from defect in SampleData.AllDefects
where defect.Status != Status.Closed
where defect.AssignedTo == tim
select defect.Summary;
# equals to:
SampleData.AllDefects.Where(defect=> defect.Status!=Status.Closed)
.Where(defect=> defect.AssignedTo==tim)
.Select(defect=> defect.Summary);
# 11.3.2
from defect in SampleData.AllDefects select defect;
# equals to:
SampleData.AllDefects.Select(defect=>defect);
from defect in SampleData.AllDefects
where defect.Status != Status.Closed
where defect.AssignedTo == SampleData.Users.TesterTim
select defect;
# equals to:
SampleData.AllDefects.Where(defect=>defect.Status!=Status.Closed)
.Where(defect=>defect.AssignedTo == SampleData.User.TesterTim);
# 注意以上三组(包括11.3.1中的例子)查询表达式与对应的转换代码的区别
User tim = SampleData.Users.TesterTim;
var query = from defect in SampleData.AllDefects
where defect.Status != Status.Closed
where defect.AssignedTo == tim
orderby defect.Serverity descending
select defect;
query = from defect in SampleData.AllDefects
where defect.Status != Status.Closed
where defect.AssignedTo == tim
orderby defect.Serverity descending, defect.LastModified
select defect;
# equals to:
SampleData.AllDefects.Where(defect=>defect.Status!=Status.Closed)
.Where(defect=>defect.AssignedTo == tim)
.OrderByDescending(defect=>defect.Serverity)
.ThenBy(defect=>defect.LastModified);
# 11.4
var query = from user in SampleData.AllUser
orderby user.Name.Length
select.user.Name;
foreach (var name in query)
{
Console.WriteLine("{0}: {1}", name.Length, name);
}
# 更优写法:
var query2 = from user in SampleData.AllUser
let length = user.Name.Length
orderby length
select new {Name = user.Name, Length=length};
foreach(var entry in query2)
{
Console.WriteLine("{0}: {1}", entry.Length, entry.Name);
}
# equals to:
var query2 = SampleData.AllUser
.Select(user=>new {user, length=user.Name.Length})
.Orderby(z=>z.length)
.Select(z=>new{Name=z.Name, Length=z.length});
# 11.5
# 11.5.1
var query = from defect in SampleData.AllDefects
join subscription in SampleData.AllSubscriptions
on defect.Project equals subscription.Project
select new {defect.Summary, subscription.EmailAddress};
query = from defect in SampleData.AllDefects
where defect.Status == Status.Closed
join subscription in SampleData.AllSubscriptions
on defect.Project equals subscription.Project
select new {defect.Summary, subscription.EmailAddress};
# equals to
query = from subscription in SampleData.AllSubscriptions
joint defect in (from defect in SampleData.AllDefects
where defect.Status == Status.Closed
select defect)
on defect.Project equals subscription.Project
select new {defect.Summary, subscription.EmailAddress};
# LINQ to Object中Join方法重载之一的签名:
static IEnumerable<TResult> Join<TOuter, TInner, TKey, TResult>(
this IEnumerable<TOuter> outer,
IEnumerable<TInner> inner,
Func<TOuter, TKey> outerKeySelector,
Func<TInner, TKey> innerKeySelector,
Func<TOuter, TInner, TResult> resultSelector
);
# 11.5.2
var query from defect in SampleData.AllDefects
join subscription in SampleData.AllSubscriptions
on defect.Project equals subscription.Project
into groupedSubscriptions
select new{Defect=defect,
Subscriptions=groupedSubscriptions};
var dates = new DateTimeRange(SampleData.Start, SampleData.End);
var query = from date in dates
join defect in SampleData.AllDefects
on date equals defect.Created.Date
into joined
select new {Date=date, Count=joined.Count()};
# GroupJoin方法签名:
static IEnumerable<TResult> GroupJoin<Touter, TInner, TKey, TResult>(
this IEnumerable<TOuter> outer,
IEnumerable<TInner> inner,
Func<TOuter, TKey> outerKeySelector,
Func<TInner, TKey> innerKeySelector,
Func<TOuter, IEnumerable<TInner>, TResult> resultSelector
);
# 11.5.3
var query = from user in SampleData.AllUser
from project in SampleData.AllProjects
select new {User=user, Project=project};
query = from left in Enumerable.Range(1, 4)
from right in Enumerable.Range(11, left)
select new {Left=left, Right=right};
# equals to:
query = Enumerable.Range(1,4)
.SelectMany(left=>Enumerable.Range(11, left),
(left, right)=>new{Left=left, Right=right});
query = from file in Dictionary.GetFiles(logDirectory, "*.log")
from line in ReadLines(file)
let entry =new LogEntry(line)
where entry.Type==EntryType.Error
select entry;
# 11.6
# 11.6.1
var query = from defect in SampleData.AllDefects
where defect.AssignedTo != null
group defect by defect.AssignedTo;
foreach(var entry in query)
{
Console.WriteLine(entry.Key.Name);
foreach(var defect in entry)
{
Console.WriteLine(" ({0}) {1}", defect.Serverity, defect.Summary);
}
}
query = from defect in SampleData.AllDefects
where defect.AssignedTo != null
group defect.Summary by defect.AssignedTo;
foreach(var entry in query)
{
Console.WriteLine(entry.Key.Name);
foreach(var summary in entry)
{
Console.WriteLine(" {0}", Summary);
}
Console.WriteLine();
}
# equals to
SampleData.AllDefects.Where(defect=>defect.AssignedTo !=null)
.GroupBy(defect=>defect.AssignedTo, defect=>defect.Summary);
# 11.6.2
var query = from defect in SampleData.AllDefects
where defect.AssignedTo != null
group defect by defect.AssignedTo into grouped
select new {Assignee=grouped.Key, Count=grouped.Count()};
# equals to:
var query = from grouped in (from defect in SampleData.AllDefects
where defect.AssignedTo != null
group defect by defect.AssignedTo)
select new {Assignee=grouped.Key, Count=grouped.Count()};
# equals to:
var query = SampleData.AllDefects
.Where(defect=> defect.AssignedTo!=null)
.GroupBy(defect=>defect.AssignedTo)
.Select(grouped=>new{Assignee=grouped.Key, Count=grouped.Count()});
query = from defect in SampleData.AllDefects
where defect.AssignedTo!=null
group defect by defect.AssignedTo into grouped
select new{ Assignee=grouped.Key,
Count=grouped.Count()} into result
orderby result.Count descending
select result;
# equals to:
query = SampleData.AllDefects
.Where(defect=>defect.AssignedTo!=null)
.GroupBy(defect=>defect.AssignedTo)
.select(grouped=>new{Assignee=grouped.Key, Count=grouped.Count()})
.OrderByDescending(result=>result.Count);
# 11.7
# 11.7.1
var adultNames = (from person in people
where person.Age>=18
select person.Name).ToList();
# equals to:
var adultNames = people.Where(person=>person.Age>=18).Select(person=>person.Name).ToList();
from defect in SampleData.AllDefects
join subscription in SampleData.AllSubscriptions
on defect.Project equals subscription.Project
select new {defect.Summary, subscription.EmailAddress};
# equals to
SampleData.AllDefects.Join(SampleData.AllSubscriptions,
defect=>defect.Project,
subscription=>subscription.Project,
(defect, subscription)=>new {defect.Summary, subscription.EmailAddress});
标签:c#,第十一章,SampleData,defect,LINQ,var,query,AllDefects,select 来源: https://blog.csdn.net/yiaizhuan9752/article/details/114407191