|
So either do two checks in one Where:
DateTime earliest = new DateTime(2013, 12, 1);
DateTime latest = new DateTime(2014, 1, 14);
var inYear = docs.Where(d => DateTime.Parse(d.Date) >= earliest && DateTime.Parse(d.Date) < latest); Or do two Wheres:
DateTime earliest = new DateTime(2013, 12, 23);
DateTime latest = new DateTime(2014, 1, 14);
var inYear = docs.Where(d => DateTime.Parse(d.Date) >= earliest).Where(d => DateTime.Parse(d.Date) < latest);
Now go and kick the idiot who decided string is a good storage medium...
Never underestimate the power of stupid things in large numbers
--- Serious Sam
|
|
|
|
|
Hopefully, this will give you some idea as to how to do this:
using System;
using System.Collections.Generic;
using System.Linq;
namespace LinqDateTest
{
class Program
{
private readonly Dictionary<string, MySimpleClass> _mySimpleClasses = new Dictionary<string, MySimpleClass>();
static void Main(string[] args)
{
new Program().AllocAndTest();
}
private int _index = 1;
public void AllocAndTest()
{
Alloc("10/03/2024");
Alloc("10/12/2013");
Alloc("11/12/2013");
Alloc("12/12/2013");
var results = (from p in _mySimpleClasses
let myDate = DateTime.Parse(p.Value.MyStringDate)
where myDate > DateTime.Now.Subtract(TimeSpan.FromDays(100)) && myDate < DateTime.Now
select p.Value).ToList();
foreach (var value in results)
{
Console.WriteLine("Index {0}, Value {1}", value.Index, value.MyStringDate);
}
Console.Read();
}
private void Alloc(string dateValue)
{
_mySimpleClasses.Add(dateValue, new MySimpleClass { MyStringDate = dateValue, Index = _index++ });
}
}
public class MySimpleClass
{
public string MyStringDate;
public int Index;
}
} Saying that, I really wouldn't like to push the idea of using a string to represent the date. If the string value isn't parseable using Parse, the whole Linq query will blow up. A simple extension will take care of this:
public static class DateExtensions
{
public static DateTime ToSafeDate(this string value)
{
DateTime parsedDate;
if (!DateTime.TryParse(value, out parsedDate))
{
parsedDate = DateTime.MinValue;
}
return parsedDate;
}
} This would turn the Linq query into:
var results = (from p in _mySimpleClasses
let myDate = p.Value.MyStringDate.ToSafeDate()
where myDate > DateTime.Now.Subtract(TimeSpan.FromDays(100)) && myDate < DateTime.Now
select p.Value).ToList();
|
|
|
|
|
Hi Gang,
I am having a big brain cramp and cannot find anything on this anywhere. I have a small winforms app that will allow me to create configurations and serialize them to files. The app will read the files from a directory and display them in a datagridview along with a details view for the selected item in the datagrid. I have a binding source and a binding navigator. Here is the issue. Initially there will be no configuration files so the List<Config> will be created but will be empty. This in turn disables the Navigator so the AddNewItem button is disabled so nothing can ever be added. As a work around for now, I create a blank object that has a name <ChangeMe> so that there is at least one record. Having this one record enables everything and it all works great. So my question is, how can I get it to enable when the list is empty?
Thanks!
Mondis
|
|
|
|
|
Is the list empty or is it null? I can see null disabling the navigator but not an empty list. I don't have the time to test it right now.
|
|
|
|
|
The list is empty. I have a local class level variable defined as
List<Config> configs;
In my load routine I initialize it.
configs = new List<Config>();
I then check for config files and add those to the list. If nothing is added then the empty configs list is added to the bindingsource.datasource as
bindingsource.datasource = configs;
The kluge is to add an empty item to the list and everything works,but that doesn't seem right. I can also delete that empty item and the Binding Navigator is still enabled, which doesn't make sense at all.
Thanks!
Mondis
|
|
|
|
|
OK, it was a thought. I still can't test the thing yet. That won't happen until tonight, if things go well.
|
|
|
|
|
No problem, thanks. I am racking my brain over this one. It just doesn't make any sense to me at all. If by some unforeseen reason I find an answer I will post back, but I haven't had any luck in 2 days.
Mondis
|
|
|
|
|
I wrote this line of code this morning and thought it was great. Maybe a bit confusing, but fun
orderItem.ReturnAuthNumber = order.OrderNumber + ++currentReturnAuthCount;
The three + signs in a row make me smile.
Hogan
|
|
|
|
|
Not really the right place for this: this forum is for questions rather than "I like this" type comments.
Why not take it here: http://www.codeproject.com/Lounge.aspx[^] where it might be better appreciated?
Never underestimate the power of stupid things in large numbers
--- Serious Sam
|
|
|
|
|
I was under the thought that the lounge was not for code... I even marked it as a non-question so as not to confuse anybody... Guess I'm the confused one.
Hogan
|
|
|
|
|
The Lounge isn't for programming questions rather than isn't for code: it's for talking rather than coding if you like. If it's a discussion about how it amuses you, then that's fine. If it's about how it works, or how to fix it, then that's a no-no.
This forum is for code - though talking does go on - it's deliberately more technical here.
Never underestimate the power of stupid things in large numbers
--- Serious Sam
|
|
|
|
|
He probably could've chucked this into the Weird and Wonderful too.
"I've seen more information on a frickin' sticky note!" - Dave Kreskowiak
|
|
|
|
|
I thought the Q&A was for questions. This is a 'discussion' board. Seems harsh to me.
Regards,
Rob Philpott.
|
|
|
|
|
Hogan, if you really want to blow your mind, you could end up with something like this:
int currentCount = 12;
int output = 12;
int mindBlowing = 4;
mindBlowing += output++ + ++currentCount;
|
|
|
|
|
I'd rather he didn't!
Mind you, it could be worse:
int plus = 12;
plus += plus++ + ++plus; But if I used that in actual code I should be shot!
Never underestimate the power of stupid things in large numbers
--- Serious Sam
|
|
|
|
|
That is so wrong, it's actually beautiful.
|
|
|
|
|
snorkie wrote: The three + signs in a row make me smile.
Myself I smile when I hear 3 years after I left a company that my code is the most respected because the developers that followed me have found it is the easiest to maintain.
That is me though.
|
|
|
|
|
hi,
i dont english good.
i want in win application export data to table located in web sql
in mode without connect internet.i will do export in offline mode.
best regards
|
|
|
|
|
If your database is located on a different server, you cannot export data to it without a connection.
|
|
|
|
|
i dont english good. i should connect to internet?
i will export data to sql located web host.
in offline mode i can export data in c#?
please help me
best regards.
|
|
|
|
|
In order to upload the data you have to connect to the database server. There are many ways you can do this. You could use a web service, or you could open a direct connection to that database and fire your commands directly. If, you are talking about exporting the data at the server side, then as long as you have uploaded it, you may not need an active connection at that end. Depending on what database you are using, you should be able to schedule a task that runs at the database server that exports the data.
The key thing is - you don't need to be connected to export data that exists at the client side. You only need the connection to get the data from the client to the server or to get data from the server to the client.
|
|
|
|
|
thanks guides.
i want to transfer data from local sql to server sql without connect to internet in win application
and i want transfer data in offline mode.
please help me
best regards
modified 14-Jan-14 17:53pm.
|
|
|
|
|
I'm not sure how many different ways I can say the same thing. If you want to transfer between two different servers, you have to have an active connection. It doesn't have to be a web based connection, but you do need to have a Sql connection to the remote database server, as long as that server is configured to allow you to connect directly to it.
|
|
|
|
|
OK, so if you don't have a connection between the client and the server, how are you going to get the data over there? Put it on a disk and give it to a kid with a good pair of shoes??
|
|
|
|
|
I believe it's called "Man in a Van".
|
|
|
|