LINQ SUM, AVERAGE - asp.net

This is code that returns list of data from Splatt Table.
Public Function GetListofProduct(ByVal pCOde As String, ByVal date1 As DateTime, ByVal date2 As DateTime) As List(Of Splatt)
Return (From e As Splatt In TradingCTX.Splatts Where e.plCode.Contains(pCOde) And (e.plDate = date1 Or e.plDate = date2)).ToList
End Function
I really need help about getting the sum of the price and transfer it to another gridview.

Instead of writing the Linq the way you are try like this. This will give you the sum, but you can also return your list by calling ToList() instead of Sum. You can also use Average.
Dim sum As Double = TradingCTX.Splatts.Where(Function(e) e.plCode.Contains(pCOde) And (e.plDate = date1 Or e.plDate = date2)).Sum(Function(e) e.Charges)
There is nothing wrong with writing the Linq as you have so you could keep your Linq and just change your ToList() to .Sum(Function(e) e.Charges) where e.Charges is the name of the field you want to sum.
Please explain your difficulties getting the sum value to another table.

Related

Compare date part of datetime column with NamedQuery

I have a table containing entries with date and time. I try to create a NamedQuery which only compares the date part.
#NamedQuery(name = "Vote.findForDate", query = "SELECT v FROM Vote v WHERE v.createdAt = :date")
...
createNamedQuery("Vote.findForDate").setParameter("date", date, TemporalType.DATE).getResultList();
But it seems that it always tries to compare the whole datetime.
Is there no way without using date() function in SQL?
I try to be independent from the database. For example h2 has no date() function.
One possible solution is the usage of date as column type and reduce the information.
#Column(name = "voteDate")
#Temporal(value = TemporalType.DATE)
private Date voteDate;

Convert Array list containing Datetime to String array list

I am new to Asp.Net and hence this question which I know might sound silly but I am really reaching no where.
Is there a way to convert an array list that contains 'Datetime' values to an array list of strings ? I wanted to use the output as a part of a javascript function.
Thanks for the help !
I am using Asp.Net with VB.
Updated
Here is the snippet
Dim timeLapse As New ArrayList
Dim timelapsehour As New ArrayList
Dim DtTmOfInf As DateTime
DtTmOfInf = DateTime.Parse(temp)
.
.
.
For i = 0 To timeLapse.Count - 1
timelapsehour.Add(DtTmOfInf.AddHours(timeLapse(i)))
Next
Here the 'DtTmOfInf' stores the value of 'temp' which is a string in datetime format and the timelapsehour is that array list that stores the required date and time values.
You could do this in a one-liner even (with Linq):
Dim yourDateList As List(Of DateTime) = ...
Dim yourStringList = yourDateList.Select(Function(d) d.ToString()).ToList()
I've made some assumptions about your data types; if you could add to your question the code you use to build your list, then I can refine as necessary.

Sorting an array of dates

I have created an array from a CSV of date values and now need to be able to sort them so that I can then get the latest date from the array.
I have tried:
Array.Sort()
but this doesn't sort correctly, I suppose because the array values are strings, any body got any ideas??
Thanks for any help.
CODE USED TO CREATE ARRAY
'array string exampel: "19/07/2012,23/07/2012,23/07/2012,19/07/2012,25/07/2012"
Dim ArrDates As Array = ArrDates .Split(",")
SOLUTION
Dim ArrAgentsReportCheck As Array = AgentsReportCheck.Split(",")
Dim ArrDates As New List(Of Date)
For i As Integer = 0 To ArrAgentsReportCheck.Length - 1
ArrDates.Add(ArrAgentsReportCheck(i))
Next
ArrDates.Sort()
Dim LatestDate As Date = ArrDates.Max()
ArrDates = ArrDates.OrderBy(Function(d) DateTime.ParseExact(d, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture)).ToArray()
Alternately, you can use OrderByDescending() depending upon your needs.
As astander said, It is very complicated to sort a array having datetime values. Instead just convert the array to List or ArrayList and make your life easy.
For ArrayList you can use the following syntax:
List<DateTime> dates = ... // init and fill
dates.Sort();
dates.Reverse();
One way would be to convert strings to DateTime using DateTime.ParseExact
Another way just to write your own IComparer and pass to Array.Sort

VBScript - Database - Recordset - How to pass DateDiff value in the database

I'm using DateDiff() function of ASP to find the date difference between two dates.
The Function works fine and displays the exact date difference between two dates but when it comes to insert this value in the database it takes 9 as the value irrespect of any date difference.
Suppose difference between two dates is more than 15 or 20 days, in the database it takes "9".
I have used INT as the DATA TYPE for the column where it displaying the date difference.
Is DATA TYPE creating an issue here?
I even tried using session variable to store the value but no luck - here's my code below:
if request.Form("sub") <> "" then
sql = "Select * from emp_leave_details"
rs.open sql , con, 1, 2
dim diff
dim todate
dim fromdate
fromdate= rs("leave_from")
todate= rs("leave_to")
session("date_diff")=datediff("d",fromdate,todate)
rs.addnew
rs("emp_name") = request.Form("name")
rs("emp_no") = request.Form("number")
rs("address") = request.Form("address")
rs("contact_no") = request.Form("contact")
rs("mobile_no") = request.Form("mobile")
rs("contact_onleave") = request.Form("contact_details")
rs("leave_type") = request.Form("rad")
rs("other_leave_details") = request.Form("PS")
rs("leave_from") = request.Form("from")
rs("leave_to") = request.Form("to")
rs("applied_by") = request.Form("apply")
rs("accepted_by") = request.Form("accept")
rs("approved_by") = request.Form("approve")
rs("no_of_leave_taken")= session("date_diff")
rs.update
response.Write("<script language='javascript'>{update();}</script>")
rs.close
end if
The datatype has nothing to do with this. Storing the value in the session is not the solution. You can use a regular variable.
From your code it looks like you always use the same values for fromdate and todate. This is because you do not iterate the rows in the resultset.
if not rs.bof and not rs.eof then
do while not rs.eof
'' code to execute for each row
rs.moveNext
loop
end if
In your current script rs will always return the results of the first row returned by the query.
The second problem your running into might be the Date datatype. Convert your value to a date using cDate and use this to calculate the difference.
Your problem is , you search for "Select * from emp_leave_details" which always gives all records from that table. You retrieve the values of the first record and do a diff of these, which results in always the same value, that is normal. From your question it is unclear what you really want to do. I suppose so want so select a record like
Select * from emp_leave_details where emp_name=<%=request.Form("name")%>
and based on that add a new record with a computed no_of_leave_taken.
Sorry guys my bad...
It was the database field name that I was calling instead of
fromdate= request.form("from")
todate= request.form("to")
I was calling this
fromdate= request.form("leave_from")
todate= request.form("leave_to")
Sorry again..but I really appreciate you all for providing me with all that possible solutions.
Thanks.

Datatable Compute Function for multiple columns

I want to count the number of non-null values per column in a Datatable. I could loop through the columns and use the compute function on each column, but I was wondering if there is a more efficient way to do this.
You could add a column with an expression that checks whether the rests of the columns are null, see http://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression(VS.80).aspx
Then you can Compute on that column.
I think that the Compute function is quite appropriate in this context. You could use code similar to the following:
For Each col as DataColumn in myTable
Dim aggExpr as string = string.format("Count{0}", col.ColumnName)
Dim filterExpr as string = string.format("{0} IS NULL", col.ColumnName)
Dim myCount as integer = CInt(myTable.Compute(aggExpr, filterExpr))
Console.WriteLine(myCount)
Next
(Typed in here, watch for syntax)
Note that I say "similar to the following". Please add appropriate error/null value checks.

Resources