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
Related
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.
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;
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.
I am working with ASP.net. In that in one sql query my output is 21,22,23, which is a string.
I want to remove those commas and store them as separate integer values...I want to use an array. Plese help. How to do that ?
You can convert a string separated by certain characters by using .Split(char):
string test = "21,22,23";
string[] split = test.Split(',');
This will give you an array of strings though. If you want to use them as integers you will want to convert them as well, and depending on your situation you might want to check if it's parseable or not, but you could use LINQ and do something like this:
string test = "21,22,23";
int[] values = test.Split(',').Select(value => Convert.ToInt32(value)).ToArray();
the String.Split(char[]) function should do this for you. I think in ASP.net it goes:
string values = "21,22,23";
string[] valuesArray = values.split(",");
While a normal String.Split would work, you still won't get integer values. You can try a simple LINQ query like so:
var results = from string s in yourString.Split('s')
select int.Parse(s);
You can then obviously cast it to a list or array, depending on your needs... A sample for converting it to an array directly in the LINQ query is as follows:
int[] results = (from string s in yourString.Split('s')
select int.Parse(s)).ToArray();
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.