标签:
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);
标签:
原文地址:http://www.cnblogs.com/zjmsky/p/4896180.html