I have a SQLite query to pull orders from an ORDERS table in a database:
qry.executeSql("SELECT o.ID AS orderIDx, o.token AS orderTokenx, r.retailerName AS retailerNamex FROM orders AS o LEFT JOIN retailers AS r ON r.token = o.retailerID ", [], function(tx, results){
//query was a success
var len = results.rows.length;
for (var i=0; i<len; i++){
var orderID = results.rows.item(i).orderIDx;
var orderToken = results.rows.item(i).orderTokenx;
var retailerName = results.rows.item(i).retailerNamex;
//create container for order and populate it with orderItems in the below query
qry.executeSql("SELECT * FROM orderItems WHERE orderID = '"+orderToken+"' ", [], function(tx1, results1){
var len1 = results1.rows.length;
for (var i1=0; i1<len1; i1++){
$('.orderItem.'+orderID+' p').append(results1.rows.item(i1).productName);
}
});
}
});
This is my query but with a lot of clutter taken out, ive left the essentials.
So I am trying to loop through the orders table and show order specific info in one section, and within that as a drop down I widh to displat each order item. So within the order loop I am using that ID to call orderItems etc... but it is only appending the results of the orderItem query in the last container.
I hink this is because the query is Asynchronous, but I am unsure how to popupulate the orderItem container before the next loop for the order query is carried out :/
I am struggling to do the same thing with Asynchronmous jQuery AJAX stuff.. so if this is linked then an explanation would be great.
Also, I was going to use a JOIN to join the order and orerItems tables in the outer query, but was then unsure about how to loop through the items as well as the orders within 1 loop?
To me the problem seems to be the orderID variable which changes before the orderItems get appended.
I think it will help if you would change last for loop into
var len1 = results1.rows.length;
var order_id = results1.rows.item(0).orderID;
for (var i1=0; i1<len1; i1++){
$('.orderItem.'+order_id+' p').append(results1.rows.item(i1).productName);
}
In this case you won't be using the changed variable anymore.
Related
I have an E-commerce website, I want to sort the products in product page by in-stock first so I did this and it works just fine:
$products = Product::orderByDesc('quantity')->get();
Now, I added a new column to the product table which is product_order that will take an integer so that I can custom sort the products, I tried the following code:
$products = Product::orderByDesc('quantity')->orderByDesc('product_order')->get();
The problem is that it only sorts them by quantity, the second orderByDesc is not effective, how can I make it effective?
I managed to solve the issue by doing the following:
$productsQty = Product::orderByDesc('product_order');
$products = $productsQty->orderByDesc('quantity')->get();
foreach ($products as $product) {
if($product->quantity <= 0) {
$product->product_order = 0;
$product->save();
}
}
I am creating Order including OrderDetail. When I have a Cart consisting of many Products, I create an Order, I want each product ID to be added to the OrderDetail including the OrderID.
But currently, I only get a ProductID (OrderID) in the first loop added to the OrderDetail. When I debug, the loop still executes enough times quantity of Product in Cart but is not added to OrderDetail.
So what is the problem that I am facing here?
Repository
public bool CreateOrder(CartViewModel invoiceVM, string userId)
{
invoiceVM.Invoices.CreateAt = DateTime.Now;
_dbContext.Invoice.Add(invoiceVM.Invoices);
_dbContext.SaveChanges();
decimal orderTotal = 0;
var cartItems = GetCartItem(userId);
foreach (var item in cartItems)
{
var invoiceDetail = new InvoiceDetails
{
ProductId = item.Products.Id,
InvoiceId = invoiceVM.Invoices.Id,
Price = item.Products.Price * item.Quantity,
Quantity = item.Quantity,
};
orderTotal += (item.Quantity * item.Products.Price);
_dbContext.InvoiceDetails.Add(invoiceDetail);
}
invoiceVM.Invoices.OrderTotal = orderTotal;
_dbContext.SaveChanges();
return true;
}
"but is not added to OrderDetail" You mean that the Order is created in your database, But you can't see any OrderDetail in your database ?
if
_dbContext.InvoiceDetails.Add(invoiceDetail);
.
.
_dbContext.SaveChanges();
are successfully pass with no errors, It save data in your database and I think it's your CartViewModel that is not updating.
you can add your data to CartViewModel in you loop or run another GetInvoice() from your dbContext.
I am setting up the query that fetch data from 3 tables based on filters that user select. filters can be multiple or can be none.
I made stored procedures with different combination. But I know that was the worst thing that I did.
var result = (from product in context.Products
from img in context.ProductImage
from saved in context.SavedProduct
where (cat.Color.Contains(product.Color)
& cat.BrandName.Contains(product.Brand_Name)
& cat.Fabric.Contains(product.Fabric)
& cat.Design.Contains(product.Design))
select new
{
product.ProductID,
product.Price,
product.Brand_Name,
product.Title,
product.Color,
product.Fabric,
product.Design,
img.Image,
saved.ProductSavedCounter,
}).ToList();
Product related details in a Product Table. Product images in a ProductImage table. And How many people saved this product are in SavedProduct table.
It returns the products only if user select all filters means when user select red color, Nike brand, cotton fabric etc. If one is missed than this query returns nothing. I want when 1 or 2 are missed than it should return data according to other selected fitters.
Pardon me if there is any mistake I am new bee.
And I missed the joins.
This may help:-
Note: I assumed the ProductID is in the other tables to make the joins, in general if your model have navigation properties for the Product image and save product you would not need the joins.
var result = from product in context.Products
join img in context.ProductImage on product.ProductID equals img.ProductID
join saved in context.SavedProduct on product.ProductID equals saved.ProductID
where (
(string.IsNullOrEmpty(cat.Color) || cat.Color.Equals(product.Color))
& (string.IsNullOrEmpty(cat.BrandName) || cat.Brand_Name.Equals(product.Brand_Name))
& (string.IsNullOrEmpty(cat.Fabric) || cat.Fabric.Equals(product.Fabric))
& (string.IsNullOrEmpty(cat.Design) || cat.Design.Equals(product.Design))
& (
((string.IsNullOrEmpty(cat.Color) ? 1 : 0) +
(string.IsNullOrEmpty(cat.BrandName) ? 1 : 0) +
(string.IsNullOrEmpty(cat.Fabric) ? 1 : 0) +
(string.IsNullOrEmpty(cat.Design) ? 1 : 0)) >= 2) //at least two conditions
)
select new {
product.ProductID,
product.Price,
product.Brand_Name,
product.Title,
product.Color,
product.Fabric,
product.Design,
img.Image,
saved.ProductSavedCounter,
};
I think a better solution would to build it gradually, as below:-
//build the joins
var result = (from product in context.Products
join img in context.ProductImage on product.ProductID equals img.ProductID
join saved in context.SavedProduct on product.ProductID equals saved.ProductID
select new { product, img, saved }).AsQueryable();
//get number of conditions avaliable
var conditionCount = (string.IsNullOrEmpty(cat.Color) ? 1 : 0) +
(string.IsNullOrEmpty(cat.Brand_Name) ? 1 : 0) +
(string.IsNullOrEmpty(cat.Fabric) ? 1 : 0) +
(string.IsNullOrEmpty(cat.Design) ? 1 : 0);
if (conditionCount >= 2)
{
//add the condition if they exists
if (!string.IsNullOrEmpty(cat.Color))
result = result.Where(x => cat.Color.Equals(x.product.Color));
if (!string.IsNullOrEmpty(cat.Brand_Name))
result = result.Where(x => cat.Brand_Name.Equals(x.product.Brand_Name));
if (!string.IsNullOrEmpty(cat.Fabric))
result = result.Where(x => cat.Fabric.Equals(x.product.Fabric));
if (!string.IsNullOrEmpty(cat.Design))
result = result.Where(x => cat.Design.Equals(x.product.Design));
//make the final select
var finalResult = result.Select(x => new
{
x.product.ProductID,
x.product.Price,
x.product.Brand_Name,
x.product.Title,
x.product.Color,
x.product.Fabric,
x.product.Design,
x.img.Image,
x.saved.ProductSavedCounter,
}).ToList();
}
And if you meant that whenever any filter is missing the query should ignore it remove the condition count and the if statement for it.
Been trying for a while now, but just can't get it to work the way I want. Referring to the code snippet below, I want to include a related table to Bricks (BrickColors). As of now BrickColors are not included and is lazy loaded.
var query = (from ul in DbContext.UserLocs
join l in DbContext.Locs on ul.LocId equals l.Id
join lb in DbContext.LocBricks on l.Id equals lb.LocId
join b in DbContext.Bricks on lb.BrickId equals b.Id
join bc in DbContext.BrickColors on b.ColorId equals bc.Id
where ul.UserId == userId
group new { LocQty = ul.Quantity, LocBrickQty = lb.Quantity, Brick = b } by new { b.BrickId, b.ColorId }
into data
orderby data.Key
select new
{
Brick = data.FirstOrDefault().Brick,
Quantity = data.Sum(d => d.LocBrickQty * d.LocQty)
})
.AsNoTracking();
If I remove .AsNoTracking() the performance is quite good, because it keeps the BrickColors table in memory, but I want it to be included in the query from the start.
I have tried DbContext.Bricks.Include(b => b.BrickColorAccessor) in the query, but that doesn't work. I think my group new { } is messing something up since I don't include BrickColors there...
I have 2 linq queries. First query does nothing because of unique index and this is OK. But second also does nothing while it should add records . If I bypass first query second query works. Should I refresh entity ? How ?
foreach (var product in productList)
{
cc2nexo_SubiektProduct newproduct = new cc2nexo_SubiektProduct();
newproduct.Name = product.Name;
newproduct.VAT = product.VAT;
newproduct.Id = product.Id;
foreach (var stawkaVAT in myNexo_ExitoEntities.StawkiVat)
{
if (stawkaVAT.Stawka * 100 == tryconvert_dec(newproduct.VAT))
{
newproduct.VAT_Id = stawkaVAT.Id;
}
}
myNexo_ExitoEntities.cc2nexo_SubiektProduct.Add(newproduct);
SurroundWithTryCatchDB(() =>
{
myNexo_ExitoEntities.SaveChanges();
});
}
var orders = (from myorders in myNexo_ExitoEntities.temp_SubiektOrderList
select myorders).ToList();
foreach (var order in orders)
{
cc2nexo_SubiektOrderList neworder = new cc2nexo_SubiektOrderList();
neworder.Data_utworzenia_sprawy = tryconvert_date(order.Data_utworzenia_sprawy);
neworder.Data_modyfikacji_sprawy = tryconvert_date(order.Data_modyfikacji_sprawy);
neworder.Data_umowy = tryconvert_date(order.Data_umowy);
neworder.Id = order.Id;
myNexo_ExitoEntities.cc2nexo_SubiektOrderList.Add(neworder);
SurroundWithTryCatchDB(() =>
{
myNexo_ExitoEntities.SaveChanges();
});
Debug.WriteLine(neworder.LastName);
}
I am receiving an error
Cannot insert duplicate key row in object 'dbo.cc2nexo_SubiektProduct' with unique index 'K_ID'. The duplicate key value is (1).The statement has been terminated
The reason of problem was SurroundWithTryCatchDB procedure not listed in my question. Because first query caused exception due to unique index all next SaveChanges did not work. I have changed my first query to work with unique values and now everything is OK.