i got problem with my LINQ to format date. Here is code
var resultCustomer = from row in formDg.dtCustomer.AsEnumerable()
where row.Field<int>("customerID") == customerID2
select new
{
name = row["customerName"].ToString(),
ic = row["customerIC"].ToString(),
add1 = row["customerAdd1"].ToString(),
add2 = row["customerAdd2"].ToString(),
tel = row["customerTel"].ToString(),
tel2 = row["customerTel2"].ToString(),
tel3 = row["customerTel3"].ToString(),
email = row["customerEmail"].ToString(),
dateRegister = row["customerDateRegister"].ToString(),
customerRef = row["customerRef"].ToString(),
customerGroup = row["groupCustName"].ToString(),
panelName = row["panelName"].ToString(),
};
var firstRecord = resultCustomer.First();// My record return single row only
My question is how i can format custom date like "dd/MM/yyyy" in customerDateRegister?, Because I got problem when my textbox show 16-Nov-12 12:00:00 AM. I don`t want to display time but just date only.
I have try like this tbDateRegOv.Text = firstRecord.dateRegister.ToString("dd/MM/yyyy");. not work. Then i try dateRegister = row["customerDateRegister"].ToString("dd/MM/yyyy"), in LINQ. Same problem.
So how can I solved this problem? Thanks for help. :)
You need to cast your row as a DateTime first. This will allow you to use the custom format strings for dates.
dateRegister = ((DateTime)row["customerDateRegister"]).ToString("dd/MM/yyyy")
If customerDateRegister is of type DateTime you should make it a DateTime in the first place. Therefor use the DataRow extension method Field<T>:
dateRegister = row.Field<DateTime>("customerDateRegister")
You could try this , replace your dateRegister line with this.
dateRegister = ((DateTime)row["customerDateRegister"]).ToString("dd/MM/yyyy"),
Or, you can use MaskedEditBox with the desired datetime mask
Related
So in my application I extract data from a database and then extract it to a csv file and I say what I want under each columns like so:
public List<ExtractDocument> GetExtractDocuments(List<Guid>ItemDetailIds)
{
var items = GetExtractData(ItemDetailIds);
return items.Select().Select(item => new ExtractDocument
{
ItemDetailId = item.Field<Guid>("ItemDetailID"),
ItemNumber = item.Field<string>("Number"),
ItemTitle = item.Field<string>("Title"),
ItemRevision = item.Field<string>("RevisionNumber"),
ActionType = item.Field<string>("Action"),
ActionedBy = item.Field<string>("ActionedBy"),
ActionedDate = item.Field<DateTime?>("ActionedDate")?.ToString("dd/MM/yyyy HH:mm:ss.fff"),
Comment = item.Field<string>("Comment"),
TaskType = item.Field<string>("TaskType"),
StartDate = item.Field<DateTime?>("StartDate")?.ToString("dd/MM/yyyy HH:mm:ss.fff"),
CompletedDate = item.Field<DateTime?>("CompletedDate")?.ToString("dd/MM/yyyy HH:mm:ss.fff"),
Status = item.Field<string>("Status"),
Outcome = item.Field<string>("Outcome"),
ActionerName = item.Field<string>("TaskActionedBy"),
DateActioned = item.Field<DateTime?>("ActionDate")?.ToString("dd/MM/yyyy HH:mm:ss.fff"),
ActionTaken = item.Field<string>("TaskAction"),
TaskComment = item.Field<string>("TaskComment"),
Link = item.Field<string>("URL")
}).ToList();
}
This is working but when I open up the csv file and look under the date columns such as "ActionedDate" the date is showing up in a weird way, for example one of the values should be showing as: 03/03/2022 09:07:46 but it is showing in the field as:07:46.3, why is it doing this and how I can prevent that from happening? it doesn't do this for all the fields but it does for the majority
Yeah this was a face palm moment, just realised I had the format written as: "dd/MM/yyyy HH:mm:ss.fff" when it should be "dd/MM/yyyy HH:mm:ss:fff", changing it from ss.fff to ss:fff, fixed the issue
I have a date just one "DateOfyear" and I want to check before inserting the date in the textbox is already exists in DB but using code behind in C#.
I will give a example like what I want to do, I know is not the right code or what I need, but is just a example because I don't want peoples to past or give solution with SQLCommand/SqlServer and other thing I want something like that code.
DateTime InvoiceDateFrom = new DateTime();
DateTime InvoiceDateTo = new DateTime();
InvoiceDateFrom = Convert.ToDateTime(txtDatRiferiment.Text);
InvoiceDateTo = Convert.ToDateTime(txtDatRiferiment.Text);
if (InvoiceDateFrom == InvoiceDateTo)
{
errorMessage = vea.ErrorMessage;
}
You can use the DateTime.compareTo method:
if(InvoiceDateFrom.CompareTo(InvoiceDateTo) == 0){
errorMessage = vea.ErrorMessage;
}
More info here.
I have datetime column in gridview:
settings.Columns.Add(clmn =>
{
clmn.FieldName = "InsertDate";
clmn.Caption = TsmDxWeb.Resources.InsertDate;
clmn.CellStyle.Wrap = DefaultBoolean.False;
clmn.Width = 150;
clmn.SortOrder = DevExpress.Data.ColumnSortOrder.Descending;
});
output:
and group output:
grid group data by datetime, but I want to group them by only date.
If I add clmn.Settings.GroupInterval = DevExpress.XtraGrid.ColumnGroupInterval.Date;, no data shown in grouping. Because gridview add time '00.00.000' to dates.
Also, there is a gridviewSetting that named setting.CustomColumnGroup, but there is no example. I researched a lot for an example but, I could not find anything.
How can I group records only by date without time?
settings.Columns.Add(clmn =>
{
clmn.FieldName = "InsertDate";
clmn.Caption = TsmDxWeb.Resources.InsertDate;
clmn.CellStyle.Wrap = DefaultBoolean.False;
clmn.Width = 150;
clmn.SortOrder = DevExpress.Data.ColumnSortOrder.Descending;
clmn.Settings.GroupInterval = DevExpress.XtraGrid.ColumnGroupInterval.Date;
});
"Rows are grouped by the date part of their values, the time portion is ignored"
In this question, if I need to get "date" included in the output, what changes should be made?
When I included
let dt = l.Element("Date").Value
It gives, "Object reference not set to an instance of an object"
var query = from l in doc.Descendants("L1")
let dt = l.Element("Date").Value
let id = l.Attribute("id").Value
from subject in l.Descendants("Subject")
select new
{
Date = dt,
Id = id,
SubjectName = (string)subject.Attribute("SubjectName"),
Score = (string)subject.Attribute("Score")
};
foreach (var result in query)
{
Console.WriteLine(result);
}
If l doesn't have an Date element, trying to access l.Element("Date").Value will incur an error. You can use a conditional:
var query = from l in doc.Descendants("L1")
let dt = l.Elements("date").Any()
? l.Element("date").Value
: AnyDefaultValueIWantForDate
let id = l.Attribute("id").Value
from subject in l.Descendants("Subject")
select new
{
Date = dt,
Id = id,
SubjectName = subject.Attribute("SubjectName").Value,
Score = subject.Attribute("Score").Value
};
(I also added the .Value in SubjectName and Score).
Looking at your other XML, it does not, as Mr. Skeet mentioned, have anything in the <date> elements. You will need to explicitly handle that if you aren't planning on always having data in there.
You can do something like this:
let dt = l.Element("date") == null ? string.Empty : l.Element("date").Value
Assuming l is not null then l.Element("Date") is null which would mean that one or more of your L1 elements does not have a child Date element.
The fix would depend on what you want to do with missing dates. If you want to use default(DateTime) as a "magic" date, you could do:
let dt = (l.Element("Date") == null ? default(DateTime) : l.Element("Date").Value)
I am trying to check for people in a certain range - let minage and maxage corresponding for e.g. age group with minage=18 and maxage =24 meaning I am trying to filter out the people aged between 18 and 24.Also datatype for dob of member is string ..
i am trying this
int agefrom = Convert.ToInt32(cbGEFrom.Text);
int ageto = Convert.ToInt32(cbGETo.Text);
DateTime today = DateTime.Today;
DateTime max = today.AddYears(-(agefrom + 1));
DateTime min = today.AddYears(-(ageto));
string maxage = Convert.ToString(max);
string minage = Convert.ToString(min);
var members =
from report in eclipse.members
where string.Compare(report.member_Dob,minage) >=0
where string.Compare(report.member_Dob,maxage) < 0
select report;
i dont know is this way is correct or not ..
would any pls suggest any idea ..
many thanks in advance...
int agefrom = Convert.ToInt32(cbGEFrom.Text);
int ageto = Convert.ToInt32(cbGETo.Text);
DateTime today = DateTime.Today;
DateTime maxDOB = today.AddYears(-ageTo);
DateTime minDOB = today.AddYears(-ageFrom);
var members = eclipse.members.Where(m=>m.member_Dob>=minDOB && m.member_Dob<=maxDOB);
EDIT: Query comprehension is as under
var members = from member in eclipse.members
where member.member_Dob>=minDOB && member.member_Dob<=maxDOB
select member;
Comparing dates via their string value feels horridly wrong to me.
You say the type of the member_Dob field is string; I'd say there is something wrong with your model, it should be DateTime
If you can't change the underlying model, at least convert it in code (using DateTime.Parse). Then you can simply compare DateTimes in your where clause as per the other answers:
var members = from report in eclipse.members
let dob = DateTime.Parse(report.member_Dob) // or use ParseExact
where dob >= minDOB && dob < maxDOB
select report;