Please in my controller I have this code to fetch data from two tables in the database, it fetches the data alright but Quantity and TotalAmount amount is not coming. thank you.
public ActionResult getNewOrder()
{
ASPNETMASTERPOSTEntities db = new ASPNETMASTERPOSTEntities();
var dataList = db.tblSales.Include("tblSalesDetails").ToList();
var modifiedData = dataList.Select(x => new
{
SalesDetailId = x.SalesDetailId,
Quantity = x.Quantity,
ProductId = x.ProductId,
ProductName = x.tblProduct.ProductName,
SalesId = x.SalesId,
OrderDate = x.OrderDate,
SubTotal = x.SubTotal,
DiscountPercent = x.DiscountPercent,
VatPercent = x.VatPercent,
TotalAmount = x.TotalAmount
}).ToList();
return Json(modifiedData, JsonRequestBehavior.AllowGet);
}
Tuhin PaulPosted Mar 26, 2023, 9:55 AM
We can use eager loading with the Include method to load related entities efficiently. You are already doing this, which is good. Instead of calling ToList() on the entire dataList object, you can use AsEnumerable() to load only the necessary data into memory. This can improve performance if the original query returns a large amount of data. Instead of using anonymous types in the select statement, you can create a custom model class to hold the selected properties. This can make your code more readable and maintainable.
I also added a using block to properly dispose of the db object after use.
Emmmanuel FIADUFEPosted Mar 28, 2023, 12:45 PM
Sachin quantity and TotalAmount belongs to tblSalesDetails
Emmmanuel FIADUFEPosted Mar 28, 2023, 10:33 AM
Thank you so much Mr Paul for this explanation, I really appreciate your help, I will try and revert as soon it works for me.
Emmmanuel FIADUFEPosted Mar 25, 2023, 1:46 PM
Thank you guys, I will check and revert,
The results am getting is the quantity and TotalAmount amount rows came empty
Naimish MakwanaPosted Mar 20, 2023, 3:31 PM
It seems that you are using the
Includemethod to eagerly load thetblSalesDetailstable into the query result, but you are not using the properties from thetblSalesDetailstable in your select statement. Instead, you are trying to select properties from thetblSalestable which are not available in thedataListobject.To fix this issue, you need to modify your query to include the properties from the
tblSalesDetailstable in the select statement. Here's an updated version of your code:Thanks
Sachin SinghPosted Mar 20, 2023, 11:06 AM
do the quantity and totalamount belong to tblSales and they have data in DB?
Jignesh KumarPosted Mar 20, 2023, 11:02 AM
What values you are getting, can you please share details ?
you can try to check like this,
https://stackoverflow.com/questions/21558277/entity-framework-retrieve-data-from-table-with-foreign-key