Entity Framework学习六:一些常用查询技术

var people = context.People
.Where(p => p.PersonState == PersonState.Active)
.OrderBy(p => p.LastName)
.ThenBy(p => p.FirstName)
.Select(p => new
{
p.LastName,
p.FirstName,
p.PersonType.TypeName
});
foreach (var person in people)
{
Console.WriteLine("{0} {1} {2}",
person.FirstName, person.LastName, person.TypeName);
}
var people = context.People
.Where(p => p.PersonState == PersonState.Active)
.OrderBy(p => p.LastName)
.ThenBy(p => p.FirstName)
.Select(p => new
{
p.LastName,
p.FirstName,
p.PersonType.TypeName
});
foreach (var person in people)
{
Console.WriteLine("{0} {1} {2}",
person.FirstName, person.LastName, person.TypeName);
}
var explicitQuery =
from onePerson in context.People
where onePerson.PersonState == PersonState.Active;
public class PersonInfo
{
public int PersonId { get; set; }
public string PersonType { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
var explicitQuery =
from onePerson in context.People
where onePerson.PersonState == PersonState.Active
orderby onePerson.LastName, onePerson.FirstName
select new PersonInfo
{
LastName = onePerson.LastName,
FirstName = onePerson.FirstName,
PersonType = onePerson.PersonType.TypeName,
PersonId = onePerson.PersonId,
Phones = onePerson.Phones.Select(ph=>ph.PhoneNumber)
};
foreach (var person in explicitQuery)
{
Console.WriteLine("{0} {1} {2} {3}",
person.FirstName, person.LastName,
person.PersonType, person.PersonId);
foreach (var phone in person.Phones)
{
Console.WriteLine(" " + phone);
}
}

min:

var min = context.People.Min(p => p.BirthDate);

count:

var min = context.People.Min(p => p.BirthDate);
var query = from onePerson in context.People
where onePerson.PersonState == PersonState.Active
select new
{
onePerson.HeightInFeet,
onePerson.PersonId
};
query = query.OrderBy(p => p.HeightInFeet);
var sum = query.Sum(p => p.HeightInFeet);
Console.WriteLine(sum);
var query = from onePerson in context.People
where onePerson.PersonState == PersonState.Active
select new
{
onePerson.HeightInFeet,
onePerson.PersonId
};
query = query.OrderBy(p => p.HeightInFeet);
var sum = query.Sum(p => p.HeightInFeet);
Console.WriteLine(sum);
query = from onePerson in context.People
where DbFunctions.AddDays(onePerson.BirthDate, 2) >
new DateTime(1970,1,1)
select new{
onePerson.HeightInFeet,
onePerson.PersonId
};

分页:

var people = context.People
.OrderBy(p => p.LastName)
.Skip((criteria.PageNumber - 1) * criteria.PageSize)
.Take(criteria.PageSize);

使用Join:

var people = from person in context.People
join personType in context.PersonTypes
on person.PersonTypeId equals personType.PersonTypeId
select new
{var people = from person in context.People
join personType in context.PersonTypes
on person.PersonTypeId equals personType.PersonTypeId
select new
{
people = context.People
.Join(
context.PersonTypes,
person => person.PersonTypeId,
personType => personType.PersonTypeId,
(person, type) => new
{
Person = person,
PersonType = type
})
.Select(p => new
{
p.Person.LastName,
p.Person.FirstName,
p.PersonType.TypeName
});

分组和左连接:

var query =
from onePerson in context.People
group onePerson by new { onePerson.BirthDate.Value.Month }
into monthGroup
select new
{
Month = monthGroup.Key.Month,
Count = monthGroup.Count()
};
var methodQuery =
context.People
.GroupBy(
onePerson => new { onePerson.BirthDate.Value.Month },
monthGroup => monthGroup)
.Select(monthGroup => new
{
Month = monthGroup.Key.Month,
Count = monthGroup.Count()
});
var query =
from person in context.People
join personType in context.PersonTypes
on person.PersonTypeId equals
personType.PersonTypeId into finalGroup
from groupedData in finalGroup.DefaultIfEmpty()
select new
{
person.LastName,
person.FirstName,
TypeName = groupedData.TypeName ?? "Unknown"
};
var methodQuery = context.People
.GroupJoin(
context.PersonTypes,
person => person.PersonTypeId,
personType => personType.PersonTypeId,
(person, type) => new
{
Person = person,
PersonType = type
})
.SelectMany(groupedData =>
groupedData.PersonType.DefaultIfEmpty(),
(group, personType) => new
{
group.Person.LastName,
group.Person.FirstName,
TypeName = personType.TypeName ?? "Unknown"
});
var query =
from onePerson in context.People
from onePhone in onePerson.Phones
orderby onePerson.LastName, onePhone.PhoneNumber
select new
{
onePerson.LastName,
onePerson.FirstName,
onePhone.PhoneNumber
};
var methodQuery =
context.People
.SelectMany(person => person.Phones, (person, phone) =>
new
{
person.LastName,
person.FirstName,
phone.PhoneNumber
})
.OrderBy(p => p.LastName)
.ThenBy(p => p.PhoneNumber);

distinct:

var uniqueQuery = context.People
.Select(p => p.PersonType.TypeName)
.Distinct();

Union:

var unionQuery = context.People
.Select(p => new
{Name = p.LastName + " " + p.FirstName,
RowType = "Person"
})
.Union(context.Companies.Select(c => new
{
Name = c.CompanyName,
RowType = "Company"
}))
.OrderBy(result => result.RowType)
.ThenBy(result => result.Name);
时间: 2024-12-25 12:09:24

Entity Framework学习六:一些常用查询技术的相关文章

Entity Framework 第六篇 分页查询

目前分页支持单表 public IList<TEntity> GetPaged<TEntity>(out int total, Expression<Func<TEntity, bool>> filter = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, int index = 1, int size = 20) wh

Entity Framework 学习

Entity Framework 学习初级篇1--EF基本概况... 2 Entity Framework 学习初级篇2--ObjectContext.ObjectQuery.ObjectStateEntry.ObjectStateManager类的介绍... 7 Entity Framework 学习初级篇3-- LINQ TOEntities. 10 Entity Framework 学习初级篇4--EntitySQL. 17 Entity Framework 学习初级篇5--ObjectQ

MVC5 Entity Framework学习之弹性连接和命令拦截

到目前为止,应用程序一直在本地IIS Express上运行.如果你想让别人通过互联网访问你的应用程序,你必须将它部署到WEB服务器同时将数据库部署到数据库服务器 本篇文章中将教你如何使用在将你的应用程序部署到云环境时的Entity Framework 6的非常有价值的两个特性:弹性连接(瞬时错误的自动重试)和命令拦截(捕获所有发送到数据库的SQL查询语句并记录至日志中). 1.启用弹性连接 当你将应用程序部署到Windows Azure时,相应的数据库部也应被部署到Windows Azure S

Entity Framework 学习第二天 续

今天来写一点不一样的删除,修改,查询 下面只写了几个方法 1 /// <summary> 2 /// 根据删除条件进行删除 3 /// </summary> 4 /// <param name="removeWhere"></param> 5 public void remove(System.Linq.Expressions.Expression<Func<StudentInfo, bool>> removeWh

Entity Framework 学习第一天 续

改写第一天的增删改查方法,观察增删改查的本质 1 using System; 2 using System.Collections.Generic; 3 using System.Data.Entity.Infrastructure; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 8 namespace EFConsole 9 { 10 class Program 11 { 12 publi

Entity Framework学习中级篇

1-EF支持复杂类型的实现 本节,将介绍如何手动构造复杂类型(ComplexType)以及复杂类型的简单操作. 通常,复杂类型是指那些由几个简单的类型组合而成的类型.比如:一张Customer表,其中有FristName和LastName字段,那么对应的Customer实体类将会有FristName和LastName这两个属性.当我们想把FirstName和LastName合成一个名为CustomerName属性时,此时,如果要在EF中实现这个目的,那么我们就需要用到复杂类型. 目前,由于EF不

MVC5 Entity Framework学习之实现主要的CRUD功能

在上一篇文章中,我们使用Entity Framework 和SQL Server LocalDB创建了一个MVC应用程序,并使用它来存储和显示数据.在这篇文章中,你将对由 MVC框架自己主动创建的CRUD(create, read, update, delete)代码进行改动. 注意:通常我们在控制器和数据訪问层之间创建一个抽象层来实现仓储模式.为了将注意力聚焦在怎样使用实体框架上.这里暂没有使用仓储模式. 在本篇文章中,要创建的web页面: watermark/2/text/aHR0cDovL

MVC5 Entity Framework学习之异步和存储过程

在之前的文章中,你已经学习了如何使用同步编程模型来读取和更新数据,在本节中你将学习如何实现异步编程模型.异步可以使应用程序执行更有效率,因为它可以更有效的使用服务器资源. 同样在本节中你还将学习如何针对实体的insert, update, 和delete操作使用存储过程. 最后将应用程序部署到 Windows Azure. 下面是完成后的页面 为什么要使用异步代码 一个web服务器的可用线程是有限的,在高负载情况下,所有的可用线程可能都在被使用.当出现这种情况时,服务器将无法处理新的请求,直到有

ADO.NET Entity Framework学习笔记(3)ObjectContext

ADO.NET Entity Framework学习笔记(3)ObjectContext对象[转] 说明 ObjectContext提供了管理数据的功能 Context操作数据 AddObject 添加实体 将实体添加到集合中, 创建实体时,状态为EntityState.Detached 当调用AddObject将实体添加到Context时,状态为EntityState.Added myContext context = new myContext(); myTab r = new myTab(