• Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
  • Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!

Another C# query (and believe me I have searched)

Collapse
X
  •  
  • Filter
  • Time
  • Show
Clear All
new posts

    Another C# query (and believe me I have searched)

    I have a LINQ query that I need to insert null dates into.

    var query = (
    from MyTable in InTable.AsEnumerable()
    group MyTable by MyTable.Field<String>("ClientAccountId") into g

    select new
    {
    Activity = OutputActivity,
    GroupMember = OutputGroupMember,
    BranchNo = OutputBranchNo,
    EarmarkingID = "",
    CounterpartyAcronym = GetClientMapping(ClientMapping, g.Key),
    ProductType = OutputProductType,
    StartDate = DateTime.Now,
    EndDate = nulldate
    }

    Currently nulldate is a string containing "nulldate" that I interpret later in the process.

    Is there a way to actually have a null datetime value in the query result?

    Don't flame if you think you are doing my job for me, just FO and don't reply.

    Where's Gentile when you need her.
    Never has a man been heard to say on his death bed that he wishes he'd spent more time in the office.

    #2
    Originally posted by Scrag Meister View Post
    I have a LINQ query that I need to insert null dates into.

    var query = (
    from MyTable in InTable.AsEnumerable()
    group MyTable by MyTable.Field<String>("ClientAccountId") into g

    select new
    {
    Activity = OutputActivity,
    GroupMember = OutputGroupMember,
    BranchNo = OutputBranchNo,
    EarmarkingID = "",
    CounterpartyAcronym = GetClientMapping(ClientMapping, g.Key),
    ProductType = OutputProductType,
    StartDate = DateTime.Now,
    EndDate = nulldate
    }

    Currently nulldate is a string containing "nulldate" that I interpret later in the process.

    Is there a way to actually have a null datetime value in the query result?

    Don't flame if you think you are doing my job for me, just FO and don't reply.

    Where's Gentile when you need her.
    You can use a nullable datetime as below. Then later in your code you just include if (EndDate.HasValue) etc

    Code:
    var query = (
                                from MyTable in InTable.AsEnumerable()
                                group MyTable by MyTable.Field<String>("ClientAccountId") into g
    
                                select new
                                {
                                    Activity = OutputActivity,
                                    GroupMember = OutputGroupMember,
                                    BranchNo = OutputBranchNo,
                                    EarmarkingID = "",
                                    CounterpartyAcronym = GetClientMapping(ClientMapping, g.Key),
                                    ProductType = OutputProductType,
                                    StartDate = DateTime.Now,
                                    EndDate = new DateTime?()
                                 }

    Comment


      #3
      Originally posted by Jaws View Post
      You can use a nullable datetime as below. Then later in your code you just include if (EndDate.HasValue) etc

      Code:
      var query = (
                                  from MyTable in InTable.AsEnumerable()
                                  group MyTable by MyTable.Field<String>("ClientAccountId") into g
      
                                  select new
                                  {
                                      Activity = OutputActivity,
                                      GroupMember = OutputGroupMember,
                                      BranchNo = OutputBranchNo,
                                      EarmarkingID = "",
                                      CounterpartyAcronym = GetClientMapping(ClientMapping, g.Key),
                                      ProductType = OutputProductType,
                                      StartDate = DateTime.Now,
                                      EndDate = new DateTime?()
                                   }
      It accepted the Nullable datetime at the point of assignment but later I get

      DataSet does not support System.Nullable<>

      A few articles mentioned implement some kind of extender? to handle this as the Dataset doesn't handle it by default. Will keep searching

      Edit : Found it, had to adjust my object shredder, that transfers my var Query to a DataTable. This is the change to allow use with Nullable fields. Will read it through in detail to understand what it does.


      Code:
      foreach (PropertyInfo p in type.GetProperties())
                  {
      
                      if (!_ordinalMap.ContainsKey(p.Name))
                      {
      
                          Type colType = p.PropertyType;
      
                          if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>)))
                          {
      
                              colType = colType.GetGenericArguments()[0];
      
                          }
      
                          DataColumn dc = table.Columns.Contains(p.Name) ? table.Columns[p.Name]
      
                              : table.Columns.Add(p.Name, colType);
      
                          _ordinalMap.Add(p.Name, dc.Ordinal);
      
                      }
      
                  }
      Last edited by Scrag Meister; 14 November 2012, 17:04.
      Never has a man been heard to say on his death bed that he wishes he'd spent more time in the office.

      Comment


        #4
        I don't have a .NET environment where I am at the moment so can't help.

        Does this help? c# - linq on nullable datetime - Stack Overflow

        Comment


          #5
          Originally posted by RasputinDude View Post
          I don't have a .NET environment where I am at the moment so can't help.

          Does this help? c# - linq on nullable datetime - Stack Overflow
          Thanks, looks like I may have posted my edit when you posted your response.

          Have found a solution.
          Last edited by Scrag Meister; 15 November 2012, 08:33.
          Never has a man been heard to say on his death bed that he wishes he'd spent more time in the office.

          Comment

          Working...
          X