Click here to Skip to main content
15,887,214 members
Home / Discussions / C#
   

C#

 
AnswerRe: C# bound column in datagridview Pin
GuyThiebaut16-Jan-14 6:20
professionalGuyThiebaut16-Jan-14 6:20 
AnswerRe: C# bound column in datagridview Pin
Rahul VB31-Jan-14 7:21
professionalRahul VB31-Jan-14 7:21 
QuestionLINQ / lambda expression to check if date is within a range Pin
littleGreenDude14-Jan-14 4:59
littleGreenDude14-Jan-14 4:59 
AnswerRe: LINQ / lambda expression to check if date is within a range Pin
OriginalGriff14-Jan-14 5:08
mveOriginalGriff14-Jan-14 5:08 
GeneralRe: LINQ / lambda expression to check if date is within a range Pin
littleGreenDude14-Jan-14 5:15
littleGreenDude14-Jan-14 5:15 
GeneralRe: LINQ / lambda expression to check if date is within a range Pin
Dave Kreskowiak14-Jan-14 5:26
mveDave Kreskowiak14-Jan-14 5:26 
GeneralRe: LINQ / lambda expression to check if date is within a range Pin
OriginalGriff14-Jan-14 5:36
mveOriginalGriff14-Jan-14 5:36 
AnswerRe: LINQ / lambda expression to check if date is within a range Pin
Pete O'Hanlon14-Jan-14 5:20
mvePete O'Hanlon14-Jan-14 5:20 
Hopefully, this will give you some idea as to how to do this:
C#
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:
C#
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:
C#
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();

QuestionBinding Navigator is disabled with empty List<T> Pin
Mondis14-Jan-14 3:09
Mondis14-Jan-14 3:09 
AnswerRe: Binding Navigator is disabled with empty List<T> Pin
Dave Kreskowiak14-Jan-14 5:19
mveDave Kreskowiak14-Jan-14 5:19 
GeneralRe: Binding Navigator is disabled with empty List<T> Pin
Mondis14-Jan-14 6:44
Mondis14-Jan-14 6:44 
GeneralRe: Binding Navigator is disabled with empty List<T> Pin
Dave Kreskowiak14-Jan-14 7:27
mveDave Kreskowiak14-Jan-14 7:27 
GeneralRe: Binding Navigator is disabled with empty List<T> Pin
Mondis14-Jan-14 7:49
Mondis14-Jan-14 7:49 
GeneralI love the C# syntax Pin
snorkie14-Jan-14 2:29
professionalsnorkie14-Jan-14 2:29 
GeneralRe: I love the C# syntax Pin
OriginalGriff14-Jan-14 3:57
mveOriginalGriff14-Jan-14 3:57 
GeneralRe: I love the C# syntax Pin
snorkie14-Jan-14 6:20
professionalsnorkie14-Jan-14 6:20 
GeneralRe: I love the C# syntax Pin
OriginalGriff14-Jan-14 8:08
mveOriginalGriff14-Jan-14 8:08 
GeneralRe: I love the C# syntax Pin
Paul Conrad14-Jan-14 15:32
professionalPaul Conrad14-Jan-14 15:32 
GeneralRe: I love the C# syntax Pin
Rob Philpott14-Jan-14 22:38
Rob Philpott14-Jan-14 22:38 
GeneralRe: I love the C# syntax Pin
Pete O'Hanlon14-Jan-14 7:25
mvePete O'Hanlon14-Jan-14 7:25 
GeneralRe: I love the C# syntax Pin
OriginalGriff14-Jan-14 8:06
mveOriginalGriff14-Jan-14 8:06 
GeneralRe: I love the C# syntax Pin
Pete O'Hanlon14-Jan-14 8:16
mvePete O'Hanlon14-Jan-14 8:16 
GeneralRe: I love the C# syntax Pin
jschell14-Jan-14 13:05
jschell14-Jan-14 13:05 
Questionhow to export data to sql uploaded in web Pin
fatemehsoleimani13-Jan-14 23:51
fatemehsoleimani13-Jan-14 23:51 
AnswerRe: how to export data to sql uploaded in web Pin
Pete O'Hanlon13-Jan-14 23:57
mvePete O'Hanlon13-Jan-14 23:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.