I'm trying to display the amount of sold shoes in a chart.
I have four tables such as tblCategory, tblProduct, tblTransaction and tblTransactionItem.
I have three categories (MenShose, WomanShose, and KiddsShose as you see in the picture).
On the X-axis I want to display the category name and shose size like for example Wemanshose-35 there "WomenShe is categoryName and 35 is the size of the shose."
I tried this but it is giving me the wrong result.
public DisplaySales()
{
InitializeComponent();
chart1.Series.Clear();
var series = chart1.Series.Add("Series1");
series.XValueMember = "ProduktId";
series.YValueMembers = "TolatlSold";
series.Name = "Shose";
chart1.ChartAreas["ChartArea1"].AxisX.Interval = 1;
chart1.Series["Shose"].IsValueShownAsLabel = true;
series.CustomProperties = "LabelStyle=Left";
ShowTodaysSoldProduct();
}
// And here is what I'm trying but it shows me the wrong result. Please Help!
private void ShowTodaysSoldProduct()
{
using (Db db = new Db())
{
DateTime today = DateTime.Today;
DateTime tomorrow = today.AddDays(1);
var result = (from tr in db.TransactionItems
join t in db.Transactions on tr.TransactionId equals t.TransactionId
//join p in db.Products on tr.ProductId equals p.ProductId
join c in db.Categories on tr.CategoryId equals c.CategoryId
where t.TransactionDate >= today && t.TransactionDate < tomorrow
group tr by tr.categories.CategoryName into g
select new ProductSaled
{
ProduktId = g.Key, // Before g.Key I want to display CategoryName too
TolatlSold = g.Count()
}).ToList();
chart1.DataSource = result;
chart1.DataBind();
chart1.Show();
}
}
Related
im required to make a query to db to fetch data fro a highchart widget in our site here is the code I use currently,
var highChartsData =
(from a in db.roomdetails
join b in db.ApplySchedule on a.RoomId equals b.RoomID
where b.Status == true
select new HighlinePie
{
Title = a.RoomName,
Date = b.MDate,
Value = db.ApplySchedule.Where(x => x.RoomID == a.RoomId).GroupBy(x=>x.MDate).Count(),
}).ToList();
The problem with this approach is right now get the total count but what i need is the count based on date, for example if there was two entry on date 12/09/20201 and three entry on 14/09/20201 the data should be "Title,12/09/20201,2","Title,14/09/20201,3".
You have to use grouping:
var groupQuery =
from a in db.roomdetails
join b in db.ApplySchedule on a.RoomId equals b.RoomID
where b.Status == true
group b by new { a.RoomName, b.MDate } into g
select new HighlinePie
{
Title = g.Key.RoomName,
Date = g.Key.MDate,
Value = g.Count()
};
var highChartsData = groupQuery.ToList();
I'm struggling with what is a rather simple SQL select statement. How can this be translated into LINQ?
select
o.IdOrder, Date, s.suma, name, adresa
from
Clients c
join
Orders o on (c.IdClient = o.IdClient)
join
(select IdOrder, sum(price) suma
from OrderProduct
group by IdOrder) s on (o.IdOrder = s.IdOrder);
If you could point me in the right direction, I would greatly appreciate it.
This is what I have so far:
var y = from w in db.OrderProducts
group w by w.IdOrder into TotaledOrder
select new
{
IdOrder = TotaledOrder.Key,
price = TotaledOrder.Sum(s => s.price)
};
var i = 0;
var cc = new dynamic[100];
foreach (var item in y)
{
cc[i++] = db.Orders.Where(t => t.IdOrder == item.IdOrder)
.Select(p => new
{
IdOrder = item.IdOrder,
price = item.price,
}).Single();
}
Your SQL doesn't really give an idea on your underlying structure. By a guess on column names:
var result = from o in db.Orders
select new {
IDOrder = o.IDOrder,
Date = o.Date,
Suma = o.OrderProduct.Sum( op => op.Price),
Name = o.Client.Name,
Adresa = o.Client.Adresa
};
(I have no idea what you meant by the loop in your code.)
i want to know how to select those record which is not in another table.
I have a query to show all the stock in a period of time
var query = (from stk in ie.stocks
join s in ie.staffs on stk.stk_createby equals s.stf_id
where stk.stk_createdate >= startDate &&
stk.stk_createdate <= endDate
select new
{
StockID = stk.stk_id,
RefNo = stk.stk_ref_id,
Weight = stk.stk_weight,
Grade = stk.stk_grade,
Remark = stk.stk_remark,
StaffName = s.stf_name
}).ToList();
And also i have another query to show all delivered stock.
var query2 = (from ol in ie.orderLists
join stk in ie.stocks on ol.ol_stockid equals stk.stk_id
join dl in ie.deliveries on ol.ol_dlyid equals dl.dly_id
join s in ie.staffs on stk.stk_createby equals s.stf_id
where dl.dly_delivery_date >= startDate &&
dl.dly_delivery_date <= endDate
select new
{
StockID = stk.stk_id,
RefN = stk.stk_ref_id,
Weight = stk.stk_weight,
Grade = stk.stk_grade,
Remark = stk.stk_remark,
StaffName = s.stf_name
}).ToList();
So what i want is to show the remain stock which is not deliver. How to exclude all stock in query2?
Try Except method.
ex)
var ret = query1.Except(query2);
Actually I want to return the data from different lists based on Date. When i'm using this i'm getting data upto #Var result but i'm unnable to return the data. The issue with this is i'm getting error #return result. I want to return the data #return result. I'm using Linq C#. Can anyone help me out?
public List<CustomerWiseMonthlySalesReportDetails> GetAllCustomerWiseMonthlySalesReportCustomer()
{
var cbsalesreeport = (from cb in db.cashbilldescriptions
join c in db.cashbills on cb.CashbillId equals c.CashbillId
join p in db.products on cb.ProductId equals p.ProductId
select new
{
Productamount = cb.Productamount,
ProductName = p.ProductDescription,
CashbillDate = c.Date
}).AsEnumerable().Select(x => new ASZ.AmoghGases.Model.CustomerWiseMonthlySalesReportDetails
{
Productdescription = x.ProductName,
Alldates = x.CashbillDate,
TotalAmount = x.Productamount
}).ToList();
var invsalesreeport = (from inv in db.invoices
join invd in db.invoicedeliverychallans on inv.InvoiceId equals invd.InvoiceId
select new
{
Productamount = invd.Total,
ProductName = invd.Productdescription,
InvoiceDate = inv.Date
}).AsEnumerable().Select(x => new ASZ.AmoghGases.Model.CustomerWiseMonthlySalesReportDetails
{
Productdescription = x.ProductName,
Alldates = x.InvoiceDate,
TotalAmount = x.Productamount
}).ToList();
var abc = cbsalesreeport.Union(invsalesreeport).ToList();
var result = (from i in abc
group i by new { Date = i.Alldates.ToString("MMM"), Product = i.Productdescription } into grp
select new { Month = grp.Key, Total = grp.Sum(i => i.TotalAmount) });
**return result;**
}
You can either convert your result to a List before returning it using return result.ToList() or make your method return an IEnumerable<CustomerWiseMonthlySalesReportDetails> instead of List.
As your result is an enumeration of anonymous types you have to convert them to your CustomerWiseMonthlySalesReportDetails-type first:
select new CustomerWiseMonthlySalesReportDetails{ Month = grp.Key, Total = grp.Sum(i => i.TotalAmount) });
Assuming your type has exactly the members returned by the select.
EDIT: So your code should look like this:
var result = (from i in abc
group i by new { Date = i.Alldates.ToString("MMM"), Product = i.Productdescription } into grp
select new CustomerWiseMonthlySalesReportDetails{ Month = grp.Key, Total = grp.Sum(i => i.TotalAmount) });
return result.ToList();
You can assume Alldates property if is date of one of groups that month of date is in right place:
var result = (from i in abc
group i by new { Date = i.Alldates.ToString("MMM"), Product = i.Productdescription }
into grp
select new CustomerWiseMonthlySalesReportDetails{
Productdescription = grp.Key.Product,
TotalAmount = grp.Sum(i => i.TotalAmount),
Alldates =grp.First(i=>i.Alldates ) })
.ToList();
I am trying to sort a listbox by the customerID and then by Total (discount*unitPrice*quantity) and cannot manage to organize the code in a way that will sort it in that way. Any help would be greatly appreciated.
HERE is a link showing an image on how the results should be returned as.
var load1 = System.IO.File.ReadAllLines(#"c:\temp\AS3Products.csv")
.Select(x => new
{
CID = x.Split(',')[0],
discount = x.Split(',')[2].Trim(),
productId = x.Split(',')[0].Trim()
});
var load2 = System.IO.File.ReadAllLines(#"c:\temp\AS3Transactions.csv")
.Select(x => new
{
productId = x.Split(',')[3],
unitPrice = x.Split(',')[4],
quantity = x.Split(',')[5]
});
var querypractice = from x in load1
join y in load2 on x.productId equals y.productId
where x.CID == "110"
orderby x.discount, y.quantity
select new { x.CID, x.discount, x.productId, y.quantity, y.unitPrice };
foreach (var x in querypractice)
{
double total = double.Parse(x.quantity) * double.Parse(x.unitPrice) * double.Parse(x.discount);
listBox1.Items.Add(x.CID+ " " +x.discount+" "+x.quantity+ " " + total);
}
Disclaimer: I don't have VS on this machine, so this isn't validated, but I think you can do it using the LET statement to set up the calculated value, then order based on it.
var querypractice = from x in load1
join y in load2 on x.productId equals y.productId
let total = x.discount*x.unitPrice*x.quantity
where x.CID == "110"
orderby x.CID, total
select new { x.CID, total };
http://www.codeproject.com/Articles/231164/Into-and-let-in-LINQ-Let-vs-Into
If you're positive that these files have numbers in the expected places all the time, you could parse them as you read them from the files. Otherwise, you'll want to do some validation first or you'll get exceptions.
(I changed double.Parse to decimal.Parse - it's more accurate for manipulating dollar amounts.)
var load1 = System.IO.File.ReadAllLines(#"c:\temp\AS3Products.csv")
.Select(x => new
{
CID = int.Parse(x.Split(',')[0]),
discount = decimal.Parse(x.Split(',')[2].Trim()),
productId = int.Parse(x.Split(',')[0].Trim())
});
var load2 = System.IO.File.ReadAllLines(#"c:\temp\AS3Transactions.csv")
.Select(x => new
{
productId = int.Parse(x.Split(',')[3]),
unitPrice = decimal.Parse(x.Split(',')[4]),
quantity = int.Parse(x.Split(',')[5])
});
Then you can create your list like this. (I removed the specific id you had in your query.)
var orderedList = (from x in load1
join y in load2 on x.productId equals y.productId
let total = (x.discount * y.unitPrice * y.quantity)
orderby x.CID descending, total descending
select new
{
x.CID,
x.discount,
x.productId,
y.quantity,
y.unitPrice
});