Im having this weird problem with creating Json dynamically.... for some reason this doesnt work
var jsonData = new
{
total = totalPages,
page = page,
records = totalRecords,
rows = (
from company in companies
select new
{
i = company.Id,
cell = new string[] { company.Id.ToString(), ""+company.Name.ToString()+"" }
}).ToArray()
};
Its giving me a weird "Could not translate expression....... into sql" exception
but with this minor change it works just fine
var jsonData = new
{
total = totalPages,
page = page,
records = totalRecords,
rows = (
from company in companies
select new
{
i = company.Id,
cell = new string[] { company.Id.ToString(), ""+company.Name.ToString()+"" }
}).ToArray()
};
Notice that the change is just to make id=5 instead of dynamic.
also, this works correctly but I dont like it.
var jsonData = new
{
total = totalPages,
page = page,
records = totalRecords,
rows = (
from company in companies
select new
{
i = company.Id,
cell = new string[] { company.Id.ToString(), ""+company.Name.ToString()+"" }
}).ToArray()
};
I'm not sure if this will solve your problem, but assuming companies is an IQueryable from the DataContext, try calling ToList() on it so that the select expression doesn't get sent to the database.
var jsonData = new
{
total = totalPages,
page = page,
records = totalRecords,
rows = (
from company in companies.ToList()
select new
{
i = company.Id,
cell = new string[] { company.Id.ToString(), ""+company.Name.ToString()+"" }
}).ToArray()
};
It's trying to translate the i to SQL and failing. The query that gets generated is something close to
SELECT companies.Id,
'' + companies.Name + ''
FROM companies
Basically, your string concatenation is being sent to your SQL server.
Related
Let's suppose we have a Linq query like this:
int companyID = Convert.ToInt32(((UserIdentity)User.Identity).CompanyId);
var stock = from i in _stockService.GetStock()
join ur in _inventoryService.GetInventory() on i.ProductID equals ur.Id
where ur.ComapnyId == companyID
select new StockVM
{
Product = ur.ItemName,
Quantity = i.Quantity,
BatchID = i.BatchID,
StockStatus = i.StockStatus,
MfgDate = i.MfgDate,
ExpDate = i.ExpDate,
};
Result
How to do a "Group By Product" with sum of Quantity in this linq query?
I need to only get max ExpDate firstOrDefault
try something like this:
int companyID = Convert.ToInt32(((UserIdentity)User.Identity).CompanyId);
var stock = from i in _stockService.GetStock()
join ur in _inventoryService.GetInventory() on i.ProductID equals ur.Id
where ur.ComapnyId == companyID
group new { Quantity = i.Quantity } by new { ur.ItemName } into g
select new { Product = g.Key, TotalQuantity = g.Sum() } ).ToList() ;
List<StockVM> _lst = new List<StockVM>();
foreach(var item in stock ){
StockVM row = new StockVM();
row.Product = item.ItemName;
//....
_lst.Add(row);
}
I am working on ASP.NET MVC project. This is my code :
orderData_Entities ordertable = new orderData_Entities();
DataTable upCSV = (DataTable)TempData["UploadedCSV"];
if (isHeaderAccepted)
{
string[] data = null;
int i = 1;
foreach (DataRow dr in upCSV.Rows)
{
ObjectParameter getemail = new ObjectParameter("email", dr["email"].ToString());
ObjectParameter getpassword = new ObjectParameter("password", dr["password"].ToString());
ObjectParameter getMobile = new ObjectParameter("Mobile", dr["Mobile"].ToString());
var results = ordertable.usp_AppUpdateUserData(idSubAccount,idcategory, getemail.Value.ToString(), getpassword.Value.ToString(), getMobile.Value.ToString());
ordertable.SaveChanges();
i++;
}
}
return RedirectToAction("Index", "BulkUpload");
Here I am looping through all rows and updating results to my table. Currently what's happening here is if I have three rows with email, password and mobile, it is looping through all these three rows one by one and I can see results in debug mode, but while updating to my table, it is updating last row results to all three rows.
So can someone tell me what mistake I am doing here?
Update :
My Stored Procedure is as follows :
ALTER PROCEDURE [dbo].[usp_AppUpdateUserData]
#idSubAccount int = 0,
#idcategory int = 0,
#email nvarchar(100) =0,
#password nvarchar(50) = 0,
#Mobile nvarchar(50) = 0,
AS
BEGIN
Begin
Update tblCustomer
Set
email = #email, password = #password, mobileNo = #Mobile,
where idSubAccount = #idSubAccount and idCategory = #idcategory
END
End
GO
How can I do a where in a query on a list or table ?
To explain, I have a multiselect listbox where the user can select one or many values which are passed to my action. After that, I get all this values in a list and I want to do a Where on it like that :
List<string> CondCR = new List<string>();
foreach (var testCR in SubCR)
{
CondCR.Add(testCR);
}
ViewBag.CondCR = CondCR;
var query = (from i in items
where i.Field<String>("TIMING").Contains(GetTIMING) && i.Field<String>("CD_CR").Equals(CondCR)
select new Suivi{CD_CR = i.Field<String>("CD_CR"), CD_APPLI = i.Field<String>("CD_APPLI"), CD_TRT = i.Field<String>("CD_TRT"), LB_TRT = i.Field<String>("LB_TRT"),
PERIODE = i.Field<Int64>("PERIODE"), CD_JOB = i.Field<String>("CD_JOB"), LB_JOB = i.Field<String>("LB_JOB"), CD_TYP_TRT = i.Field<String>("CD_TYP_TRT"),
CD_TRT_SSIS = i.Field<String>("CD_TRT_SSIS"), DT_DEB = i.Field<DateTime>("DT_DEB"), DT_FIN = i.Field<DateTime>("DT_FIN"), DUREE = i.Field<String>("DUREE"),
TIMING = i.Field<String>("TIMING")
}).ToList();
return View(query);
SubCR contains the value that the user select in the list box, SubCR is of type string[].
I've tried to do a where on my list CondCR but it returns nothing and I don't know if it comes from my query or from an other thing.
Have you some suggestions ?
Okay, I've find the answer, I just had to do this in my query :
where CondCR.Contains(i.Field<String>("CD_CR").Trim()) && CondAppli.Contains(i.Field<String>("CD_APPLI").Trim())
var getAllProducts = _productService.GetAllProducts();
if (productstest.Count > 0)
{
model.idproduct.Add(new SelectListItem()
{
Value = "0",
Text = _localizationService.GetResource("Common.All")
});
foreach (var m in getAllProducts)
model.idproduct.Add(new SelectListItem()
{
Value = m.Id.ToString(),
**Text = m.Size.Distinct().ToString(),**
Selected = model.Pid == m.Id
});
}
public virtual IList<Product> GetAllProducts(bool showHidden = false)
{
var query = from p in _productRepository.Table
orderby p.Name
where (showHidden || p.Published) &&
!p.Deleted
select p;
var products = query.ToList();
return products;
}
The issue is even i tried to populate the select list with distinct size using: Text = m.Size.Distinct().ToString(), but it shows the duplicate for instance 100 products are of size 33 cm , the list will populate the dropdownlist in the view with 33cm occuring 100 times , I dont want to show 100 times , just want to show 1 time, Can any one assist me with this issue ?
Presumably you are only trying to show one product of each different size... if so initialising your getAllProducts variable like so will do the trick:
var getAllProducts = _productService.GetAllProducts().GroupBy(p => p.Size).Select(g => g.First());
I have an mvc project with a jqgrid that has an editting column, my problem is when i Try to link the edit in each row to a pop up window, when I add the code below a window pop up opens but I recieve a 402 unothorized error, here is my code in the controller
var jsonData = new
{
total = totalPages,
page = page,
records = totalRecords,
rows = (
from item in items
select new
{
id = item.Id,
cell = new string[] {
"<A HREF=javascript: void(0) onclick=window.open
('/Views/viewname/Edit/'+"item.code+",'Edit','width=700,height=800'); >Edit</A>",
column2,
column3,
etc ...}
}).ToArray()
};
but if I just link the edit to a regular page through this code
var jsonData = new
{
total = totalPages,
page = page,
records = totalRecords,
rows = (
from item in items
select new
{
id = item.Id,
cell = new string[] {
"Edit " ,
column2,
column3,
etc ...}
}).ToArray()
I do not have any problems, I have a edit page which works fine if it is not a pop up window, has anybody faced this problem, or can anyone direct me to an example that is similar.Thnaks in advance
Have you tried with quotes around your href and onclick attribute values?