LINQ orderby issue
-
Hi, I have the following code:
var query = from a in ds.Tables\[0\].AsEnumerable() from b in ds.Tables\[2\].AsEnumerable() where a.Field<string>("CaseId") == txtCaseID.Text && a.Field<string>("Password") == txtPassword.Text where a.Field<int>("Case\_Id") == b.Field<int>("Notes\_Id") orderby b.Field<string>("NoteCreated") descending select new { NoteCreated = b.Field<string>("NoteCreated"), NoteContent = b.Field<string>("NoteContent"), }; gv\_CaseDetails.DataSource = query.ToList(); gv\_CaseDetails.DataBind();
I need to order the results descending as can be seen, the problem is that the NoteCreated value is a Date but it's passed as a string. Now if I orderby NoteCreated descending it orders the results using only the first 2 digits. Example: The NoteCreated format is 30/01/2008. The above code will orderby the 30 only. Can someone please tell me how I can convert/cast the value to datetime so that I can use it? Thank you!!
Illegal Operation
-
Hi, I have the following code:
var query = from a in ds.Tables\[0\].AsEnumerable() from b in ds.Tables\[2\].AsEnumerable() where a.Field<string>("CaseId") == txtCaseID.Text && a.Field<string>("Password") == txtPassword.Text where a.Field<int>("Case\_Id") == b.Field<int>("Notes\_Id") orderby b.Field<string>("NoteCreated") descending select new { NoteCreated = b.Field<string>("NoteCreated"), NoteContent = b.Field<string>("NoteContent"), }; gv\_CaseDetails.DataSource = query.ToList(); gv\_CaseDetails.DataBind();
I need to order the results descending as can be seen, the problem is that the NoteCreated value is a Date but it's passed as a string. Now if I orderby NoteCreated descending it orders the results using only the first 2 digits. Example: The NoteCreated format is 30/01/2008. The above code will orderby the 30 only. Can someone please tell me how I can convert/cast the value to datetime so that I can use it? Thank you!!
Illegal Operation
You can use the "let" keyword in linq to set a variable to a DateTime value and then do your ordering on that variable.
var query ....
where ...
let D =
...
orderby D
... -
Hi, I have the following code:
var query = from a in ds.Tables\[0\].AsEnumerable() from b in ds.Tables\[2\].AsEnumerable() where a.Field<string>("CaseId") == txtCaseID.Text && a.Field<string>("Password") == txtPassword.Text where a.Field<int>("Case\_Id") == b.Field<int>("Notes\_Id") orderby b.Field<string>("NoteCreated") descending select new { NoteCreated = b.Field<string>("NoteCreated"), NoteContent = b.Field<string>("NoteContent"), }; gv\_CaseDetails.DataSource = query.ToList(); gv\_CaseDetails.DataBind();
I need to order the results descending as can be seen, the problem is that the NoteCreated value is a Date but it's passed as a string. Now if I orderby NoteCreated descending it orders the results using only the first 2 digits. Example: The NoteCreated format is 30/01/2008. The above code will orderby the 30 only. Can someone please tell me how I can convert/cast the value to datetime so that I can use it? Thank you!!
Illegal Operation
var query = from a in ds.Tables[0].AsEnumerable()
from b in ds.Tables[2].AsEnumerable()
where a.Field("CaseId") == txtCaseID.Text && a.Field("Password") == txtPassword.Text
where a.Field("Case_Id") == b.Field("Notes_Id")
orderby Convert.ToDateTime(b.Field("NoteCreated")) descending
select new
{
NoteCreated = b.Field("NoteCreated"),
NoteContent = b.Field("NoteContent"),
};