This question is locked. New answers and comments are not allowed.
Dear all,
please have a look at this Linq Qyery:
DateTime start =
new
DateTime(2014,1,1);
DateTime end =
new
DateTime(2014,12,1);
Guid address = Guid.Parse(
"16CF8E4F-8735-4E2E-BFDF-2A2E0C816A3B"
);
var result = from o
in
Orders
join ol
in
OrderLines
on o.Id equals ol.OrderId
join ad
in
Addresses
on o.AddressId equals ad.Id
where o.Date >= start && o.Date <= end && o.AddressId == address
group
new
{ ol.ProductLine, ad.Name1, ol.Price }
by
new
{ol.ProductLine, ad.Name1 } into gr
select
new
{
ProductLine = gr.Key.ProductLine,
Sum = gr.Sum(x => x.Price),
Address = gr.Key.Name1
};
Between the Orders and the OrderLines I have defined a relation in the MetadataSource:
public
void
PrepareOrderRelationConfiguration(MappingConfiguration<Order> configuration)
{
// new Relation (One to Many)
configuration
.HasAssociation<OrderLine>(x => x.OrderLines)
.HasFieldName(
"OrderLines"
)
.WithOpposite(x => x.Order)
.ToColumn(
"OrderId"
)
.HasConstraint((y, x) => x.OrderId == y.Id)
.IsManaged()
.WithDataAccessKind(DataAccessKind.ReadWrite);
}
public
void
PrepareOrderLineRelationConfiguration(MappingConfiguration<OrderLine> configuration)
{
// new Relation (Many to One, Master)
configuration
.HasAssociation<Order>(x => x.Order)
.HasFieldName(
"Order"
)
.WithOpposite(x => x.OrderLines)
.ToColumn(
"OrderId"
)
.HasConstraint((x, y) => x.OrderId == y.Id)
.IsRequired()
.IsManaged()
.WithDataAccessKind(DataAccessKind.ReadWrite);
}
Relation in OrderLine class:
private
Order Order;
[DataMember]
public
virtual
Order Order
{
get
{
return
Order;
}
set
{
if
(Order != value)
{
Order = value;
OnPropertyChanged();
}
}
}
private
Guid OrderId;
[DataMember]
public
Guid OrderId
{
get
{ {
return
OrderId; } }
set
{
if
(OrderId != value)
{
OrderId = value;
OnPropertyChanged();
}
}
}
Relation Order class:
private
TrackedBindingList<OrderLine> OrderLines =
new
TrackedBindingList<OrderLine>();
public
virtual
TrackedBindingList<OrderLine> OrderLines
{
get
{
return
this
.OrderLines;
}
}
Now my two questions:
Is the Join for OrderLines in the Linq Query necessary, or can it be done in a better way?
I do not have a relation between Orders and Addresses (because I do not wanna have all orders when I query an address), do you recommend one?
Thank you,
Manuel