RavendB: Select and GroupBy - cannot use Map/Reduce Index - ravendb

I've got following data structure.
public class Usage
{
public string Id { get; set;}
public string CatalogNumber { get; set;}
public string ProductDescription { get; set;}
public string ProductType { get; set;}
public DateTime DeliveryDate { get; set;}
public int AmountSold { get; set;}
}
I would like to have the result grouped by {CatalogNumber}, {ProductDescription}, {ProductType} within a specific {DeliveryDate}
ie. I'd like to query the total amount sold for a specific product within 2013-01-01 to 2013-04-04.
Expected result:
{
{
CatalogNumer: 12345
ProductDescription: Product1
ProductType: TV
TotalAmountSold: 125
}
{
CatalogNumer: 8797
ProductDescription: Product2
ProductType: Fridge
TotalAmountSold: 13
}
}
Unfortunately I cannot use a Map/Reduce index because there's no way to pass parameters to the index.
Any ideas?

You don't need to pass parameters to the index. First create a map/reduce index grouped by DeliveryDate. Then you query that index with the desired date range.

Related

WindowsForms-EFCore SQL Exception Invalid Column Name

I have a two table products and categories. When I add a product to products, I get an error. I share the codes.
class Products
{
public int Id { get; set; }
public string Name { get; set; }
public float Price { get; set; }
public string Description { get; set; }
public int CategoryId { get; set; }
public string CategoryName { get; set; }
public Categories Category { get; set; }
}
With this method, I get the products. After I fill a datagridview. But I want to see categoryName instead of CategoryId. It works, I see the categoryName instead of CategoryId in datagridview.
public List<Products> GetProducts()
{
var products = context.Products.Include(x =>x.Category ).Select(m => new Products()
{
Id = m.Id,
Name = m.Name,
Price = m.Price,
Description = m.Description,
CategoryName=m.Category.Name
}).ToList();
return products;
}
After that I have an Add method
public void AddProduct(Products products )
{
context.Products.Add(products);
context.SaveChanges();
}
However, when I try to add a new product, I have an error.
The issue is that Category Name is not in the physical table, just your object. So when EF attempts to generate the SQL, it can't find a column called CategoryName.
Take a look at this question
Exclude Property on Update in Entity Framework

What should I type in migration if I want to multiply 2 values from two different tables ? ASP.NET MVC Entity Framework

This is what am I suppose to complete "Calculate total animal maintenance cost. Which is obtained by adding the food cost (food cost * animal weight), housing cost." I don't know how to do calculation for all (PetWeight from PetRescued Table)*(FoodCost from PetSpecies Table) and Insert it into my MaintenanceCost Table.
And
For example, I have 4 type of pet species which is Cat,Dog,Rabbit and Rodent. The HousingCost for the pets are 10,20,30,40.This is Table 1. For Table 2 which is the PetRescued, How do I calculate the sum of HousingCost for how many cats/dogs/.../... that I have in my PetRescued database and Insert it into my MaintenanceCost Table.
This is my PetRescued entity which is also one of the Table
public class PetRescued
{
public int Id { get; set; }
public string PetName { get; set; }
public int PetAge { get; set; }
public string PetGender { get; set; }
public double PetWeight { get; set; }
public DateTime? DateWhenRescued { get; set; }
public string PetBreedName { get; set; }
public PetSpecies PetSpecies { get; set; }
public byte PetSpeciesId { get; set; }
public PetSanctuary PetSanctuary { get; set; }
public byte PetSanctuaryId { get; set; }
}
This is my PetSpecies entity which consist of the FoodCost and also my second Table
public class PetSpecies
{
public byte Id { get; set; }
public string SpeciesName { get; set; }
public double FoodCost { get; set; }
public double HousingCost { get; set; }
}
This is my MaintenanceCost entity which is use to store the result from the calculation
public class MaintenanceCost
{
public byte Id { get; set; }
public double TotalFoodCost { get; set; }
public double TotalHousingCost { get; set; }
public double TotalVetBillCost { get; set; }
}
The query you need is something like this. It inserts one entity containing sum of all costs.
INSERT INTO MaintenanceCost (TotalFoodCost,TotalHousingCost,TotalVetBillCost)
Values (
(SELECT SUM(PetRescued.PetWeight * PetSpecies.FoodCost FROM PetRescued INNER JOIN PetSpecies ON PetRescued.PetSpeciesId = PetSpecies.Id)),
(SELECT SUM(PetSpecies.HousingCost FROM PetRescued INNER JOIN PetSpecies ON PetRescued.PetSpeciesId = PetSpecies.Id)),
"the amount of TotalVetBillCost"
)
However this approach is against one the basic rules of databases that is never store a data that can be retrieved from other data stored in the database. Also, It is clear that when the data in other two tables alter, this entity will no longer be valid.

DataGrid with small rows and no data after binding to the ItemSource with LINQ query

I'm new to LINQ and WPF and I'm trying to bind a custom query from LINQ and the rows barely appear although when debugging the IEnumerable<OrderSummary> orderSummary has info in it. I believe the empty rows showing are the amount of rows returned from the query:
XAML:
<DataGrid Name="dgrOrders" Margin="59,54,161,285" />
Code Behind:
OrderITDataClassesDataContext dc = new OrderITDataClassesDataContext();
IEnumerable<OrderSummary> orderSummary = dc.ExecuteQuery<OrderSummary>("SELECT * FROM [Order]",1);
dgrOrders.ItemsSource = orderSummary;
public class OrderSummary
{
int OrderId { get; set; }
DateTime OrderDate { get; set; }
int CustomerId { get; set; }
}
I suppose you should use properties instead of fields. Check the examples on MSDN
public class OrderSummary
{
public int OrderId { get; set; }
public DateTime OrderDate { get; set; }
public int CustomerId { get; set; }
}
The second thing you may try - specify the dgrOrders DataContext, not the ItemSource.

nhibernate group by select column based on restrictions

I have what seems to be a rather simple problem, that I cannot figure out right now.
I have a table like so:
id | type | value | comment | date
1 1 22 test dec 2nd
2 1 23 foo dec 4th
3 2 2 bar dec 1st
Based on the model
class MyClass
public virtual long Id { get; set;}
public virtual long Type { get; set;}
public virtual long Value { get; set;}
public virtual string comment { get; set;}
public virtual DateTime Date { get; set;}
I need to group by type and select the row having the most recent date.
(That is, fetch rows with ID 2 and ID 3).
Can someone provide a Criteria with explanation of how to do this ?
two roundtrips, 1 to get the type/date of the desired rows and 1 combined to get each row. FutureValue<> will combine each Query in the loop to one roundtrip and the select converts the FutureValues to the actual values.
class TypeDate
{
public long Type { get; set; }
public DateTime Date { get; set; }
}
var groups = session.CreateCriteria<MyClass>()
.SetProjection(Projections.ProjectionList()
.Add(Projections.GroupProperty("Type"), "Type")
.Add(Projections.Max("Date"), "Date"))
.SetResultTransForm(Transformers.AliasToBean<TypeDate>());
.List<TypeDate>();
List<IFutureValue<MyClass>> futures = new List<IFutureValue<MyClass>>(groups.Count);
foreach (var group in groups)
{
futures.Add(session.CreateCriteria<MyClass>()
.Add(Restrictions.Eq("Type", group.Type))
.Add(Restrictions.Eq("Date", group.Date))
.FutureValue<MyClass>());
}
IEnumerable<MyClass> results = futures.Select(future => future.Value).ToList();

Nhibernate/Hibernate, lookup tables and object design

I've got two tables. Invoice with columns CustomerID, InvoiceDate, Value, InvoiceTypeID (CustomerID and InvoiceDate make up a composite key) and InvoiceType with InvoiceTypeID and InvoiceTypeName columns.
I know I can create my objects like:
public class Invoice
{
public virtual int CustomerID { get; set; }
public virtual DateTime InvoiceDate { get; set; }
public virtual decimal Value { get; set; }
public virtual InvoiceType InvoiceType { get; set; }
}
public class InvoiceType
{
public virtual InvoiceTypeID { get; set; }
public virtual InvoiceTypeName { get; set; }
}
So the generated sql would look something like:
SELECT CustomerID, InvoiceDate, Value, InvoiceTypeID FROM Invoice WHERE CustomerID = x AND InvoiceDate = y
SELECT InvoiceTypeID, InvoiceTypeName FROM InvoiceType WHERE InvoiceTypeID = z
But rather that having two select queries executed to retrieve the data I would rather have one. I would also like to avoid using child object for simple lookup lists. So my object would look something like:
public class Invoice
{
public virtual int CustomerID { get; set; }
public virtual DateTime InvoiceDate { get; set; }
public virtual decimal Value { get; set; }
public virtual InvoiceTypeID { get; set; }
public virtual InvoiceTypeName { get; set; }
}
And my sql would look something like:
SELECT CustomerID, InvoiceDate, Value, InvoiceTypeID
FROM Invoice INNER JOIN InvoiceType ON Invoice.InvoiceTypeID = InvoiceType.InvoiceTypeID
WHERE CustomerID = x AND InvoiceDate = y
My question is how do I create the mapping for this?
I've tried using join but this tried to join using CustomerID and InvoiceDate, am I missing something obvious?
Thanks
If your goal is (as you said) to avoid two queries, you can retrieve the data using a single HQL statement:
select i, it from Invoice i fetch join i.type it where ...
...as documented in the hibernate docs. This should execute only one SQL select statement and retrieve everything without any mapping changes.
This is a regular HQL query and is executed as follows:
IQuery q = s.CreateQuery("select i, it from Invoice i fetch join i.type it where ...");
IList invoices = q.List();
More information is available on the hibernate query language page.