I have this dates into my DGV:
14/06/2012
15/07/2012
16/07/2012
17/07/2012
I set the Filter to this: ([supplier_invoice_date] >= '13/07/2012') AND ([supplier_invoice_date] <= '17/07/2012')
The Filter return this (All the dates):
14/06/2012
15/07/2012
16/07/2012
17/07/2012
Another test:
Filter: ([supplier_invoice_date] >= '15/07/2012') AND ([supplier_invoice_date] <= '17/07/2012')
Result:
15/07/2012
16/07/2012
17/07/2012
Filter: ([supplier_invoice_date] < '17/06/2012')
Result:
14/06/2012
15/07/2012
16/07/2012
I think it is only taking the days and does not take months.
Here is my code:
Dim dt As New DataTable
Dim suppliersinvoices_data_query As String = ("DATE_FORMAT(MIN(supplier_invoice_date), '%d/%m/%Y') AS supplier_invoice_date, ...")
Dim invoice_objDataAdapter As New MySqlDataAdapter(suppliersinvoices_data_query, objConn)
invoice_objDataAdapter.Fill(dt)
Dim MyFilter As New DataView(dt)
MyFilter.RowFilter = "([supplier_invoice_date] >= '13/07/2012') AND ([supplier_invoice_date] <= '17/07/2012')"
invoicesresults_datagrid_search_supplierinvoice.DataSource = MyFilter
This is the DataView.RowFilter syntax for Dates:
dataView.RowFilter = "supplier_invoice_date >= #2012-07-13#"
But you could also using Linq-To-DataSet. I assume that the actual datatype of the field isStringinstead of Date, so you need to parse it to Date first:
Dim startDate = New Date(2012, 7, 13)
Dim endDate = New Date(2012, 7, 17)
Dim invoiceDate As Date
Dim filtered = From row In dt.AsEnumerable()
Where Date.TryParse(row.Field(Of String)("supplier_invoice_date"), invoiceDate) _
AndAlso invoiceDate >= startDate AndAlso invoiceDate <= endDate
Dim tblFiltered As DataTable = filtered.CopyToDataTable()
It looks like you are converting the dates to strings in order to format them. Try to avoid that, and just let the grid use the date value it is getting from the data source.
If you want to format the date of the DataGridView control, try it like this:
dgv1.Columns("YourDateColumn").DefaultCellStyle.Format = "dd/MM/yyyy"
Then you can use real dates to filter the data:
MyFilter.Filter = String.Format("[supplier_invoice_date] > '{0}'", _
New DateTime(2012, 7, 1))
Related
I am trying to display a message based on a date range. If it falls between the BeginDate and EndDate then the date should be display and if the date falls on 10-09-2017 then another message should be displayed, otherwise then it should display the date. This seems to fail and go directly to the else statement. My eyes are not picking up the error. How can something get displayed between a date range in VBScript.
<%
Dim DateT
Dim BeginDate
Dim EndDate
BeginDate = Day("2017-05-26")
EndDate = Day("2017-11-04")
DateT = Day(Date)
If BeginDate >= DateT =< EndDate
THEN response.write(DateT)
ElseIF BeginDate = Day("2017-10-09")THEN
response.write(DateT)
Else
response.write(DateT)
End If
%>
I believe your syntax is incorrect and your logic is overcomplicated.
Not exactly sure of the syntax here or the correct date format but I added some response.write for you to check (which should have been your first stop)
Your logic is overcomplicated because the only time it doesn't display the date is when it matches that special date..
Dim DateT
Dim BeginDate
Dim EndDate
BeginDate = CDate("2017-05-26")
EndDate = CDate("2017-11-04")
DateT = Date()
' remove these when you're finished debugging
response.write(BeginDate)
response.write(EndDate)
If BeginDate = CDate("2017-10-09")THEN
response.write("Another message")
Else
response.write(DateT)
End If
I have datatable which it first row is headers
I need specific column for this datatable according to the header
I know how to get the column if know its index.
The problem is how to get the index
Dim columnIndex as integer
Dim headerRow As DataRow = dt.Rows(0)
Dim colHeader As string ="abc"
columnIndex=???
Dim result = dt.Rows.Cast(Of DataRow)().[Select](Function(row) row(columnIndex)).Distinct().ToList()
Thanks
You may use the dt.Rows.IndexOf
Dim ValueToSearch AS string = ...
columnIndex = dt.AsEnumerable().Where(Function(x) x.Field(of String)(colHeader) = ValueToSearch).Select(Function(x) dt.Rows.IndexOf(x)).SingleOrDefault()
The above will work if the where clause returns only one or zero rows. If this is not true then you may dismiss the SingleOrDefault and then loop through the results, ie:
columnIndex = dt.AsEnumerable().Where(Function(x) x.Field(of String)(colHeader) = ValueToSearch).Select(Function(x) dt.Rows.IndexOf(x))
For Each i in columnIndex
Console.WriteLine("Value found in row with index " + i.ToString())
Next
Giannis
using the following query
Dim allEvents As New List(Of Events)
Dim digits = "1 2 3 4 5 6 7 8 9 0".Split()
Dim results = (From t In ctx.events
Where t.date = todaysdate.Date AndAlso
Not digits.Any(Function(d) (t.symbol.Contains(d)))
Order By t.time
Select New With {
t.symbol,
t.date, t.time,
t.description,
t.domestic_call_num,
t.web_url})
If results.Count > 0 Then
For x As Integer = 0 To results.Count - 1
Dim newevent As New Events
With newevent
.CompanySymbol = results(x).symbol
.EventDate = results(x).date
.EventTime = Left(results(x).time.ToString(), 5)
.EventDescription = results(x).description
If results(x).domestic_call_num IsNot Nothing Then
.DialInNumber = results(x).domestic_call_num
Else
.DialInNumber = ""
End If
If results(x).web_url IsNot Nothing Then
.WebCastUrl = results(x).web_url
Else
.WebCastUrl = ""
End If
End With
allEvents.Add(newevent)
Next
Return allEvents
I get the results I want, but i'd like to further order them by description and time
I tried the following, which should have worked
Dim results = (From t In ctx.events
Where t.date = todaysdate.Date AndAlso
Not digits.Any(Function(d) (t.symbol.Contains(d)))
Order By t.time
Group By t.description
Select New With {
t.symbol,
t.date, t.time,
t.description,
t.domestic_call_num,
t.web_url})
I also tried ThenBy which the compiler didnt like. Any help on how to accomplish this would be appreciated. Essentially I want a sort order of time then description then symbol.
When using integrated syntax, you'd write this as:
Order By t.time, t.description
Select New With {
...
For details, see the Query Expression Examples for ThenBy.
You can try ordering your items at the end of the query like that :
Dim results = (From t In ctx.events
Where t.date = todaysdate.Date AndAlso
Not digits.Any(Function(d) (t.symbol.Contains(d)))
Select New With {
t.symbol,
t.date, t.time,
t.description,
t.domestic_call_num,
t.web_url}).OrderBy(Function(r) r.time)
.ThenBy(Function(r) r.description)
It's probably faster to do it in the query tho.
Don't think this is a repost, difficult to search for the word between because it is used in everything (like searching for AND).
I want to filter a list based on a date range.
I have a list with some dates and I want to filter them by a date range. Is there a Linq or Lambda equivalent of the between statement in SQL.
For example, the code below will not work in Linqpad (or Visual Studio):
void Main()
{
List<ListExample> list = new List<ListExample>();
list.Add(new ListExample("Name1","23 Aug 2010"));
list.Add(new ListExample("Name2","23 Aug 2009"));
var query = from l in list
where l.DateValue between "01 Jan 2010" and "01 Jan 2011"
select l;
}
public class ListExample
{
public ListExample(string name, string dateValue)
{
Name = name;
DateValue = DateTime.Parse(dateValue);
}
public string Name{get;set;}
public DateTime DateValue{get;set;}
}
Something like this?
var query = from l in list
where l.DateValue >= new DateTime(2010, 1, 1)
&& l.DateValue <= new DateTime(2011, 1, 1)
select l;
You can write your own extension method:
public static bool IsBetween(this DateTime dt, DateTime start, DateTime end)
{
return dt >= start && dt <= end;
}
In which case the query would look something like (method syntax for a change):
var start = new DateTime(2010, 1, 1);
var end = new DateTime(2011, 1, 1);
var query = list.Where(l => l.DateValue.IsBetween(start, end));
I see you've provided some samples with the dates as strings. I would definitely keep the parsing logic (DateTime.ParseExactor other) separate from the query, if at all possible.
var query = from l in list
where new DateTime(1,1,2010) <= l.DateValue and DateValue <= new DateTime(1,1,2011)
select l;
of course, normally warning about timezones and different times on clients and servers apply
Datetime DT1 = DateTime.Parse("01 Jan 2010");
Datetime DT2 = DateTime.Parse("01 Jan 2011");
var query = from l in list
where l.DateValue >= DT1 && l.DateValue <= DT2
select l;
in linq you use the && and || like you would in a normal boolean statement of C#.
I can't seem to find any good reference on this. I have a lot of data in SQL with dates. So I wanted to make a line chart to show this data over time. If I want to show it over a period of days then I need to group by days.. But the LOGDATE is the full date.. not the DAY..
So I have this below, but LINQ doesn't know what 'DayOfYear' property is...
var q = from x in dc.ApplicationLogs
let dt = x.LogDate
group x by new { dayofyear = dt.Value.DayOfYear } into g
select new
{
iCount = g.Count(),
strDate = g.Key
};
In EF core you can use DateTime.Date to get the date portion of a DateTime value.
In EF6 you can use DbFunctions.TruncateTime:
to return the given date with the time portion cleared
var q = from x in dc.ApplicationLogs
let dt = x.LogDate.Date
// EF6:let dt = DbFunctions.TruncateTime(x.LogDate)
group x by dt into g
select new
{
iCount = g.Count(),
strDate = g.Key
};
You want .Date to get the date part of a DateTime not DayOfyear unless you are deliberately trying to put the same day of the year from each year into the group.
Why are you using:
let dt = x.LogDate
group x by new { dayofyear = dt.Value.DayOfYear } into g
instead of just:
group x by x.LogDate.Value.DayOfYear into g
I'm not sure about this, but it's possible using an anonymous object like that in your group by clause is messing up L2S.
Here you go
let d = new DateTime(n.time.Year, n.time.Month, n.time.Day) group n by d into grp select grp