Dim
groupResults = personList.
GroupBy(
Function
(person)
New
With
{Key person.City, Key person.Country}).
Where(
(grp) grp.Count() > 1).ToList()
groupResults = personList.GroupBy(
(grp) grp.Count() > 1).
Select
(
(grp)
PersonGroup
{
.City = grp.Key.City,
.Country = grp.Key.Country, .List = grp.ToList()
}).ToList()
groupResults = (
From person
In
personList
Group By personCityCountry =
Key .City = person.City,
Key .Country = person.Country} Into group = Group
Where group.Count() > 1
.City = personCityCountry.City,
.Country = personCityCountry.Country,
.List = group.ToList()
Public
Class
CustomerOrder
Property
Customer
As
Order
IEnumerable(Of Order)
End
GroupJoinCustomersWithCompanyNameStartsWith(
context
NorthWindContext,
startsWithValue
String
)
List(Of CustomerOrder)
context.Configuration.LazyLoadingEnabled =
True
'
' Note to use Include the following Import is needed
' Imports System.Data.Entity
results
List(Of CustomerOrder) = context.CustomerIncludes().
(customer) customer.CompanyName.StartsWith(startsWithValue)).
GroupJoin(context.Orders,
(c) c.CustomerIdentifier,
(order) order.CustomerIdentifier,
(customer, order)
Key .Customer = customer,
Key .Order = order}).
(customerData)
.Customer = customerData.Customer,
.Order = customerData.Order}).
ToList()
Return
Imports
NorthWindEntityLibrary
NorthWindEntityLibrary.Classes
NorthWindEntityLibrary.Models
Module
Module1
Private
operation
NorthOperations
Sub
Main()
standardColor = Console.ForegroundColor
Using context =
NorthWindContext
List(Of CustomerOrder) =
operation.GroupJoinCustomersWithCompanyNameStartsWith(context,
"A"
For
Each
current
Console.ForegroundColor = ConsoleColor.Green
Console.WriteLine($
"{current.Customer.CompanyName} - {current.Customer.Contact.FullName}"
Console.ForegroundColor = standardColor
order
current.Order
Console.ForegroundColor = ConsoleColor.White
" {order.OrderID}, {order.OrderDate.Value.ToShortDateString()}"
orderDetail
OrderDetail
order.OrderDetails
" {orderDetail.Product.ProductName}, {orderDetail.Quantity}"
Next
Console.WriteLine()
Using
MinMaxCountForAgeProperty()
Using context
PeopleContext
people = context.People.ToList()
query = people.GroupBy(
(person) Math.Floor(person.Age),
(person) person.Age,
(baseAge, ages)
.Key = baseAge,
.Count = ages.Count(),
.Min = ages.Min(),
.Max = ages.Max()}
output
System.Text.StringBuilder
' Iterate over each anonymous type.
result
query
output.AppendLine(vbCrLf &
"Age group: "
& result.Key)
output.AppendLine(
"Number of people in this age group: "
& result.Count)
"Minimum age: "
& result.Min)
"Maximum age: "
& result.Max)
' Display the output.
Console.WriteLine(output.ToString)
ThreeTablesGrouping(north
NorthWindContext)
query = From order
north.Orders
From customer
north.Customers.
(c)
CBool
(c.CustomerIdentifier = order.CustomerIdentifier)).DefaultIfEmpty()
From orderDetail
north.OrderDetails.Where(
(d) d.OrderID = order.OrderID).DefaultIfEmpty()
Group
Key .order = order,
Key .customer = customer,
Key .details = orderDetail
} By GroupKey =
{Key order.OrderDate.Value.Year, Key customer.CompanyName
} Into group = Group
Key .Company = GroupKey.CompanyName,
Key .OrderYear = GroupKey.Year,
Key .Amount = group.Sum(
(e) e.details.UnitPrice * e.details.Quantity)
}
queryResults = query.ToList()
Namespace
Classes
Products
Shared
List()
List(Of Product)
products
List(Of Product) From {
Product
{.Id = 1, .Make =
"Samsung"
, .Model =
"Galaxy S3"
},
{.Id = 2, .Make =
"Galaxy S4"
{.Id = 3, .Make =
"Galaxy S5"
{.Id = 4, .Make =
"Apple"
"iPhone 5"
{.Id = 5, .Make =
"iPhone 6"
{.Id = 6, .Make =
{.Id = 7, .Make =
{.Id = 8, .Make =
"HTC"
"Sensation"
{.Id = 9, .Make =
"Desire"
{.Id = 11, .Make =
{.Id = 12, .Make =
"Nokia"
"Lumia 735"
{.Id = 13, .Make =
"Lumia 930"
{.Id = 14, .Make =
{.Id = 15, .Make =
"Sony"
"Xperia Z3"
''' <summary>
''' Comparer on Make and Model properties
''' </summary>
ProductComparer
Implements
IEqualityComparer(Of Product)
Shadows
Equals(p1
Product, p2
Product)
Boolean
_
IEqualityComparer(Of Product).Equals
If
ReferenceEquals(p1, p2)
Then
ReferenceEquals(p1,
Nothing
OrElse
ReferenceEquals(p2,
False
p1.Make = p2.Make
AndAlso
p1.Model = p2.Model
GetHashCode(product
Integer
IEqualityComparer(Of Product).GetHashCode
ReferenceEquals(product,
0
hashProductName
=
(product.Make
Is
, 0, product.Make.GetHashCode())
hashProductCode
= product.Model.GetHashCode()
Xor
''' Example to first get distinct products in one query
''' followed by grouping on make property in a second query.
productList = Products.List()
Console.WriteLine(
"Original list"
productList.ForEach(
(product) Console.WriteLine(product))
"ProductComparer results"
productsQuery
IEnumerable(Of Product) =
productList.Distinct(
ProductComparer).
OrderBy(
(product) product.Make)
product
Console.WriteLine(product)
"Group by make"
grouped
IEnumerable(Of IGrouping(Of
, Product)) =
productsQuery.GroupBy(
grouping
IGrouping(Of
, Product)
Console.WriteLine(grouping.Key)
" {product}"
Console.ReadLine()
SELECT
A.*
FROM
Customers A
INNER
JOIN
CompanyName,
ContactName,
ContactTitle,
Address,
City,
PostalCode
Customers
GROUP
BY
HAVING
COUNT
(*) > 1
) B
ON
A.CompanyName = B.CompanyName
AND
A.ContactName = B.ContactName
A.ContactTitle = B.ContactTitle
A.Address = B.Address
A.City = B.City
A.PostalCode = B.PostalCode
ORDER
A.CompanyName
''' Provides a column to mark a row in a DataGridView
''' to act upon
CustomerRigger
Inherits
Process()
Identifier
CompanyName
ContactName
ContactTitle
Address
City
Exists
ReadOnly
ItemArray()
()
Get
CStr
(Identifier).PadLeft(3,
"0"
c), CompanyName, ContactName, ContactTitle, Address, City, PostalCode}
Overrides
ToString()
$
"'{CompanyName}' '{ContactName}' '{ContactTitle}' '{Address}' '{City}' '{PostalCode}'"
GetCustomerDuplicatesAsList()
List(Of CustomerRigger)
dataOperations
DataOperations
dt
DataTable = dataOperations.ReadCustomersFromDatabase()
duplicates
IEnumerable(Of IEnumerable(Of CustomerRigger)) = From dataRow
dt.AsEnumerable()
item =
Key .Identifier = dataRow.Field(Of
)(
"Identifier"
),
Key .CompanyName = dataRow.Field(Of
"CompanyName"
Key .ContactName = dataRow.Field(Of
"ContactName"
Key .ContactTitle = dataRow.Field(Of
"ContactTitle"
Key .Street = dataRow.Field(Of
"Address"
Key .City = dataRow.Field(Of
"City"
Key .PostalCode = dataRow.Field(Of
"PostalCode"
)}
grou
p temp = item By Key =
Key .CompanyName = item.CompanyName,
Key .ContactName = item.ContactName,
Key .ContactTitle = item.ContactTitle}
Into Group Where Group.Count() > 1
Group.
(g)
.Identifier = g.Identifier,
.CompanyName = g.CompanyName,
.ContactName = g.ContactName,
.ContactTitle = g.ContactTitle,
.Address = g.Street,
.City = g.City,
.PostalCode = g.PostalCode
})
(From item
duplicates From row
item
row).ToList()
personList Group By personCityCountry =
{Key .City = person.City, Key .Country = person.Country} Into group = Group
{.City = personCityCountry.City, .Country = personCityCountry.Country, .List = group.ToList()}).ToList()