I thought this would be the proper code to replace a null but it gives me an error "The name 'subt2' does not exist in the current context"
var SQLSELECT2 = "SELECT SUM(Price) FROM ProductEntered WHERE TransID=#0";
var sub2 = db.QueryValue(SQLSELECT2, transaction_id);
if(sub2 == null){
var subt2 = 0.00;
}
else{
var subt2 = sub2;
}
and in the page I have #subt2
I decided to try to explain what I am doing to hopefully get a better response. I have a page that you can add products to. It makes an entry into the table with the TransID as a reference to this page. This page pulls the SUM from Price where rows have this TransID. The problem lies when there is not a product entered in the table with this TransID. I need it to output a 0.00 when there is not data to be displayed.
took me forever but I found a few articles and put them together. This is the code:
var SQLSELECT2 = "SELECT SUM(Price) FROM ProductEntered WHERE TransID=#0";
var sub2 = db.QueryValue(SQLSELECT2, transaction_id);
if(String.IsNullOrEmpty(Convert.ToString(sub2)))
{
sub2 = 0.00;
}
I would try to replace it as follows:
var sub2 = db.QueryValue(SQLSELECT2, transaction_id);
var result = sub2 == null ? 0 : sub2;
return result;
When you define a variable by using a var (or an int, double, string, etc) it only exists in within the braces {} where it is defined. So since you are defining it inside the braces, it's not available later in the page. Better would be:
var SQLSELECT2 = "SELECT SUM(Price) FROM ProductEntered WHERE TransID=#0";
var sub2 = db.QueryValue(SQLSELECT2, transaction_id);
var subt2 = 0;
if(sub2 == System.DBNull){
subt2 = 0.00;
}
else
{
subt2 = sub2;
}
Related
I'm trying to organize a database by a rank in a certain order that I want. Normally, sort would work, but I want a couple of things to happen:
1. raw data so that filters can work for other data (i.e. by name, team, etc)
2. I don't want to always sort by rank
Ranks in order:
Captain, Officer, Chef, Crew, Recruit, Lost, Brig
Right now I'm forcing a filtered sort first alphabetically only when the rank filter is sorted, then when that is done, it sorts the list according to the above sort. The problem that I'm having is that it takes some time to execute the script. I'm thinking that this can be done much more efficiently, but I'm way rusty when it comes to this stuff. Here's my code below:
var ss = SpreadsheetApp.getActiveSpreadsheet();
var members = ss.getSheetByName("Members");
var temp = ss.getSheetByName("sortRank");
var ranks = ["Captain", "Officer", "Chef", "Crew", "Recruit", "Lost", "Brig"];
var rankData = members.getRange(1,5,members.getLastRow(),1);
var search = null;
var query = null;
var x = null;
if(order != null) {
ranks = ranks.reverse();
}
for (var i=0; i < 7; i++) {
search = rankData.createTextFinder(ranks[i]);
x = search.findAll().length;
if(x != 0) {
query = search.findNext().getRow();
members.getRange(query,1,x,37).copyTo(temp.getRange(temp.getLastRow()+1,1));
}
}
}
Sort members by rank:
Assumes rank is in column 5 of members.
var members = ss.getSheetByName("Members");
var rankData = members.getRange(1,5,members.getLastRow(),1).getValues();
var ranks = ["Captain", "Officer", "Chef", "Crew", "Recruit", "Lost", "Brig"];
var rVal={};
ranks.forEach(function(e,i){rVal[e]=i+1;});
members.getDataRange().setValues(members.getDataRange().getValues().sort(function(a,b){return rVal[a[4]]-rVal[b[4]];}));
I need to create a table in View by this View Model:
public class ApplicationContentViewModel
{
public BPMSPARS.Models.MySql.application application {get; set;}
public BPMSPARS.Models.MySql.content content { get; set; }
public BPMSPARS.Models.MySql.app_delegation app_delegation { get; set; }
}
But the query for creating new Table is very complex.
I use this query in MySQL, and I can get correct results by using it.
SELECT APP_UID, (SELECT CON_VALUE FROM content WHERE CON_CATEGORY = 'PRO_TITLE' AND CON_ID =
(SELECT PRO_UID from app_delegation WHERE del_thread_status='open' and USR_UID = '00000000000000000000000000000001' AND APP_UID = '9134216305aaaea1b67c4e2096663219')) AS TASK_NAME,
(SELECT CON_VALUE FROM content WHERE CON_CATEGORY = 'TAS_TITLE' AND CON_ID =
(SELECT TAS_UID from app_delegation WHERE del_thread_status='open' and USR_UID = '00000000000000000000000000000001' AND APP_UID = '9134216305aaaea1b67c4e2096663219')) AS PROCESS_NAME FROM app_delegation
WHERE del_thread_status='open' and USR_UID = '00000000000000000000000000000001' AND APP_UID = '9134216305aaaea1b67c4e2096663219'
But, I have to convert this query in linq or EF in MVC.
How Can I write This Query in Entity Framework query?
And How Can I display results in View?
Your SQL query seems (very) peculiar to me, as it is quite redundant. I am going to assume the sub-queries return a single value and enforce it with LINQ.
First I pulled out the common sub-query over app_delegation:
var USR_APP_Delegation = from a in app_delegation
where a.del_thread_status == "open" &&
a.USR_UID == "00000000000000000000000000000001" &&
a.APP_UID == "9134216305aaaea1b67c4e2096663219"
select a;
In LINQ it is easy to combine the two UID queries into one query:
var UIDs = (from a in USR_APP_Delegation
select new { a.PRO_UID, a.TAS_UID })
.Single();
Now you can do the name subqueries:
var TASK_NAME = (from c in content
where c.CON_CATEGORY == "PRO_TITLE" &&
c.CON_ID == UIDs.PRO_UID
select c.CON_VALUE)
.Single();
var PROCESS_NAME = (from c in content
where c.CON_CATEGORY == "TAS_TITLE" &&
c.CON_ID == UIDs.TAS_UID
select c.CON_VALUE)
.Single();
Then you can put all the queries together for the final result:
var ans = (from a in USR_APP_Delegation
select new {
a.APP_UID,
TASK_NAME,
PROCESS_NAME
})
.Single();
Again, this makes it obvious that your e.g. returning APP_UID when you know exactly what it is, and you are combining TASK_NAME and PROCESS_NAME into a query for no real advantage.
I would suggest using join against content makes a much more understandable query (even in SQL) and makes it clearer what is being returned:
var names = from a in app_delegation
join cpro in content on new { CON_ID = a.PRO_UID, CON_CATEGORY = "PRO_TITLE" } equals new { cpro.CON_ID, cpro.CON_CATEGORY }
join ctas in content on new { CON_ID = a.PRO_UID, CON_CATEGORY = "TAS_TITLE" } equals new { ctas.CON_ID, ctas.CON_CATEGORY }
where a.del_thread_status == "open" &&
a.USR_UID == "00000000000000000000000000000001" &&
a.APP_UID == "9134216305aaaea1b67c4e2096663219"
select new {
a.APP_UID,
Task_Name = ctas.CON_VALUE,
Process_Name = cpro.CON_VALUE
};
I am trying to write a query to show all records owned by the current logged on user but i am having issues inserting the "userid" variable into the string?
#{
Layout = "~/_template1.cshtml";
var db = Database.Open("StayInFlorida");
var userid = WebSecurity.CurrentUserId;
var premierproperty = "SELECT PropertyName, PropertyID FROM PropertyInfo WHERE OwnerID='userid'";
}
<h1>Properties - Page coming soon</h1>
#userid
#foreach (var row in db.Query(premierproperty)){
#row.propertyname
}
Any ideas?
Try like this:
#{
Layout = "~/_template1.cshtml";
var db = Database.Open("StayInFlorida");
var userid = WebSecurity.CurrentUserId;
var premierproperty = "SELECT PropertyName, PropertyID FROM PropertyInfo WHERE OwnerID = #0";
}
<h1>Properties - Page coming soon</h1>
#userid
#foreach (var row in db.Query(premierproperty, userid))
{
#row.propertyname
}
TSQL:-
Update table1
Set Name = 'John',
Address = null
where
ID = 1
LINQ-TO-SQL
var tab = db.Table1.Single(s => s.ID == 3);
tab.Name = DateTime.Now;
tab.Address = null;
db.SubmitChanges();
There isn't a single LINQ to SQL statement for updates. You have to retrieve the object, modify it, then save the changes (code assumes a single row since you have a specific Id):
var entity = context.Table1.Single(t => t.Id == 1);
entity.Name = "John";
entity.Address = "Toronto";
context.SubmitChanges();
using (var dataContext = new MyEntities())
{
var contact = Contacts.Single (c => c.ContactID == 1);
contact.FirstName = 'John';
contact.Address= 'Toronto';
dataContext.SaveChanges();
}
I have SQL database as follows
alt text http://img97.imageshack.us/img97/5774/dbimage.jpg
Now I want to filter the restaurant_detail table for the parameters:
1. cuisine 2. area
Can you help me to build LINQ query?
I presume you have a model generated either with LINQ to SQL or Entity Framework. Also, I'm assuming foreign key relationships have been set.
var details = db
.Cuisines
.Where(c => c.Cuisine=="something")
.SelectMany(c => c.RestaurantCuisines)
.Select(rc => rc.Restaurant.RestaurantDetails)
.Where(rd => rd.Area=="something")
;
Done with the linq query using following lines of code :
c = from q in dc.restaurant_cuisines
where q.cuisine.cuisine1.Contains(cuisine)
&& q.restaurant.price.ToString().Length == price.Length
select new NearBy { NearById = q.restaurant.id, NearByLongitude = (double)q.restaurant.longitude, NearByLatitude = (double)q.restaurant.latitude };
}
int[] ids = new int[c.Count()];
var lon = from q1 in dc.area_maps where q1.area.ToLower() == area.ToLower() select q1.longtitude;
var lat = from q1 in dc.area_maps where q1.area.ToLower() == area.ToLower() select q1.latitude;
foreach(NearBy n in c)
{
result = calcDistNew((double)lat.FirstOrDefault(), (double)lon.FirstOrDefault(), n.NearByLatitude, n.NearByLongitude);
ids[i++] = n.NearById;
}
var r = from q in dc.restaurant_details
where 1 == 1 &&
(ids).Contains(q.restaurant_id)
select new Restaurant
{
Restora_id = q.restaurant_id.ToString(),
Name = q.restaurant.name,
Foodtype = q.restaurant.foodtype.foodtype1,
Avg_rating = q.restaurant.avg_rating.ToString(),
Featured = q.restaurant.featured.ToString(),
CuisineList = getCuisine(q.restaurant_id),
Restora_type = q.type,
Distance = Math.Round(calcDistNew((double)lat.FirstOrDefault(), (double)lon.FirstOrDefault(), (double)q.restaurant.latitude, (double)q.restaurant.longitude), 2),
Newarrival = q.restaurant.newarrival.ToString(),
CountRecord = ids.Length.ToString()
};
var d = r.AsEnumerable().OrderBy(t => t.Distance);
var g = d.Take(recordSize + 10).Skip(recordSize);
return g.ToList();
Please note that above displayed code generated with some changes from the initial requirements.