I am trying to compute the string expression by DataTable.Compute.
A simple string like below is working:
static void Compare()
{
var logicalExpression = "'2022-09-14' <= '2029-12-31'";
DataTable dt = new DataTable();
var result = (bool)dt.Compute(logicalExpression, "");
}
But, the date 2022-09-14 is dynamic, it should be the date for now.
I have tried to replace '2022-09-14' with Now(),GETDATE(). None of them work.
Is there any function to get current date time in Compute?
You can do this like this:
var logicalExpression = "'" + DateTime.Now.ToShortDateString() + "' <= '2029-12-31'";
you can also specify in what format you want the date, see this post for an example.
var logicalExpression = $"#{DateTime.Now:MM/dd/yyyy}# <= #12/31/2029#";
This is the proper way to represent dates in this context. See here for more information.
Related
i am using a azure search, and i have a console app with the code as below, which is working fine.
DocumentSearchResult<Hotel> results;
Console.WriteLine("Search started\n");
results = indexClient.Documents.Search<Hotel>("smart", new SearchParameters { Top=5 });
WriteDocuments(results);
currently its searching a text with word "smart". this is straight forword, what i need is i have several fields in the table, i want to search based on the feild .
for example let i have two fields
1)Title
2)SoldDate
I have to write code for finding items which has title 'john' and which has a sold date < current date.
what should i do to achieve this?
You can achieve what you want with search and a filter:
// Approach #1
string currentDate = DateTime.UtcNow.ToString("O");
var parameters = new SearchParameters()
{
Filter = "soldDate lt " + currentDate,
Top = 5
}
results = indexClient.Documents.Search<Hotel>("john", parameters);
This will filter the documents to only those with a soldDate before currentDate, and then searches the filtered documents such that documents match if any of the searchable fields contain "john". You can narrow this down to just the title field like this:
// Approach #2
string currentDate = DateTime.UtcNow.ToString("O");
var parameters = new SearchParameters()
{
Filter = "soldDate lt " + currentDate,
SearchFields = new[] { "title" },
Top = 5
}
results = indexClient.Documents.Search<Hotel>("john", parameters);
Or like this:
// Approach #3
string currentDate = DateTime.UtcNow.ToString("O");
var parameters = new SearchParameters()
{
Filter = "soldDate lt " + currentDate,
QueryType = QueryType.Full,
Top = 5
}
results = indexClient.Documents.Search<Hotel>("title:john", parameters);
Which way you use depends on whether you want all search terms to be limited to a specific set of fields (Approach #2), or if you want specific terms to match specific fields (Approach #3).
The reference for SearchParameters is on learn.microsoft.com.
I am working ion full calendar in c# MVC .a need to read dates from database .but I can't read them in proper mode by using JSON thus I need to convert dates to following format...
Current Output Format:2014-06-01 16:00
[{"id":"d719381c-b3e4-e311-afa9-eca86bf6cf86","title":"Annual Function","start":"2014-06-01 16:00","end":"2014-06-01 19:00"},{"id":"d719381c-b3e4-e311-afa9-eca86bf6cf86","title":"convocation day","start":"2014-06-24 00:49 12:49","end":"2014-06-24 "}]
Required output Format : "/Date(1423087200000)/"
[{"id":1259,"title":"bvbvcbc","start":"/Date(1423845000000)/","end":"/Date(1423931400000)/","allDay":false,"description":"Notesbvbvcbc"},{"id":1263,"title":"om nmh sivaay ","start":"/Date(1423087200000)/","end":"/Date(1423432800000)/","allDay":false,"description":"Notesom nmh sivaay "},{"id":1265,"title":"vimal raturi","start":"/Date(1423546200000)/","end":"/Date(1423632600000)/","allDay":false,"description":"Notesvimal raturi"}
You would have to use Date() method to get the unix timestamp.
var ob = [
{"id":"d719381c-b3e4-e311-afa9-eca86bf6cf86","title":"Annual Function","start":"2014-06-01 16:00","end":"2014-06-01 19:00"},
{"id":"d719381c-b3e4-e311-afa9-eca86bf6cf86","title":"convocation day","start":"2014-06-24 00:49","end":"2014-06-24 12:49"}
];
for (var i = 0; i < ob.length; i++ ){
var unix_start = new Date(ob[i].start);
var unix_end = new Date(ob[i].end);
ob[i].start = '/Date(' + unix_start.getTime() /1000 + ')/';
ob[i].end = '/Date(' + unix_end.getTime() /1000 + ')/';
}
thanks for your answers,
i found the answer by simply writin g following code in my view under the ajax success property,
$.each(data, function (i, item)
{
item.start = new Date(item.start);
item.end = new Date(item.end);
});
I am trying to compute average of a column in a datatable RefdtClone.
The data varies quite a bit, and I want to exclude data that is excessively large, for example exclude value > 9999. I can't seem to find information about how to construct the filter string as part of the compute() argument. Any help is appreciated.
My code is as follows.
string dataColumnName = "";
string filter =""; // I want to exclude value > 9999 from compute() avg
dataColumnName = (string)RefdtClone.Columns[firstDataColumn + i].ColumnName;
ComputeAVGColumn = String.Concat("AVG(["+ dataColumnName+ "])");
Analysisdt.Rows[i]["Mean 1"] = RefdtClone.Compute(ComputeAVGColumn, filter);
string dataColumnName = (string)RefdtClone.Columns[firstDataColumn + i].ColumnName;
ComputeAVGColumn = String.Concat("AVG(["+ dataColumnName+ "])");
string filter = String.Format("[{0}] <= 9999", dataColumnName);
Analysisdt.Rows[i]["Mean 1"] = RefdtClone.Compute(ComputeAVGColumn, filter);
syntax for filter is described on MSDN DataColumn.Expression page
I have been getting an annoying littler error and cannot for the life of me figure out why it is being cause. I have an xml file where i am storing data, as shown below.
- <EmployeeFinance>
<EmployeeEmploy_Id>5584</EmployeeEmploy_Id>
<EmpPersonal_Id>30358</EmpPersonal_Id>
<No_DaysWorked>30</No_DaysWorked>
<Date_Appointment>17/02/2012</Date_Appointment>
<Date_Employment>02/05/1984</Date_Employment>
<Date_Termination>01/01/0001</Date_Termination>
<Payperiod_StartDate>01/01/2013</Payperiod_StartDate>
<Payperiod_EndDate>31/01/2013</Payperiod_EndDate>
<BatchNumber>38</BatchNumber>
<PAYE_ToDate_Computed>0</PAYE_ToDate_Computed>
<Income_Tax_RateID>0</Income_Tax_RateID>
<NIS_RateID>0</NIS_RateID>
<NIS_weeks_worked>0</NIS_weeks_worked>
</EmployeeFinance>
If you look at the date nodes, Payperiod_StartDate,Payperiod_EndDate, Date_Appointment etc. They all have the same format. Now in my C# code, when i write my query to select from the xml file i get the String was not recognized as a valid DateTime error. WHen i comment out all the other dates and leave start_date, it works. They are the same format , i cant see what i am doing wrong. Please help me.
var context = new SSPModel.sspEntities();
XElement xelement = XElement.Load(GlobalClass.GlobalUrl);
XDocument doc = XDocument.Load(GlobalClass.GlobalUrl);
var query = from nm in xelement.Elements("EmployeeFinance")
select new EmployeeEmploy
{
Employee_Personal_InfoEmp_id = (int)nm.Element("EmpPersonal_Id"),
Substantive_designation = (int)nm.Element("Position_Id"),
Grade_Id = (int)nm.Element("Grade_Id"),
PositionTotal_PtBasic = (double)nm.Element("Sum_AllPosition"),//part of basic
GradeTotal_PtBasic = (double)nm.Element("Sum_AllGrade"), //part of basic
Housing_Allowance = (double)nm.Element("Housing"),
Base_Pay = (double)nm.Element("Base_Pay"),
startDate = (DateTime)nm.Element("Payperiod_StartDate"),
endDate = (DateTime)nm.Element("Payperiod_EndDate"),
Date_of_Appointment = (DateTime)nm.Element("Date_Appointment"),
Date_of_Employment = (DateTime)nm.Element("Date_Employment"),
Termination_date_actual = (DateTime)nm.Element("Date_Termination"),
Base_Pay_Currency = (string)nm.Element("Currency"),
Exchange_rate = (double)nm.Element("Exchange_Rate")
};
var x = query.ToList();
foreach (var xy in x) {
Debug.WriteLine(xy.endDate);
}
Because 17/02/2012 is not a valid date, however, 02/17/2012 is. The date will be parsed as mm/dd/yyyy. One option is to use DateTime.ParseExact to parse a date with the dd as the first set of numbers. e.g.
var startDate = DateTime.ParseExact("17/02/2012", "dd/MM/yyyy", null);
The debugger will show you that nm.Element("Payperiod_EndDate").ToString() gives you a string that includes the xml tags for that element. Try the following instead:
startDate = DateTime.ParseExact(nm.Element("Payperiod_EndDate").Value, "dd/MM/yyyy", null)
I have a search function which includes dates also. The problem that i have is this:
when you add 2 dates in the search function for example:
2013-04-10 and 2013-04-11 it displays just for 2013-04-10 until 23:59 or if i search the same date
2013-04-10 and 2013-04-10 it doesn't find any hit.
the code is this:
Func<string, string> emptyToNull = s => String.IsNullOrWhiteSpace(s) ? null : s.Trim();
var from = emptyToNull(input.CreditApplicationSearchFromDate);
var to = emptyToNull(input.CreditApplicationSearchToDate);
from = from ?? DateTime.Today.ToString("yyyy-MM-dd");
to = to ?? DateTime.Today.ToString("yyyy-MM-dd");
And i was thinking to do like this:
Func<string, string> emptyToNull = s => String.IsNullOrWhiteSpace(s) ? null : s.Trim();
var from = emptyToNull(input.CreditApplicationSearchFromDate);
var to = emptyToNull(input.CreditApplicationSearchToDate);
from = from ?? new DateTime().ToString("yyyy-MM-dd HH:mm");
to = to ?? new DateTime().ToString("yyyy-MM-dd HH:mm");
but is not good and i don't know how to do to display when i search for example
2013-04-10 and 2013-04-10 to find the hits for all day
You need to specify the start and end hours of the whole day, or better yet - the very start of the next day:
from = from ?? DateTime.Now.Date.ToString("yyyy-MM-dd");
to = to ?? new DateTime.Now.Date.AddDays(1).ToString("yyyy-MM-dd");
Note the use of Now.Date giving you the current date without a time component (new DateTime() will not compile, so I don't really know where you got that piece of code from).