question

MikoKage avatar image
0 Votes"
MikoKage asked TimonYang-MSFT commented

Date Time Query

Can someone tell me how I can do a DateTimeQuery with LINQ and compare the dates?
I tried it like this:

private readonly string PATH = Path.Combine(HostingEnvironment.ApplicationPhysicalPath, "weatherdata.xml");

public List<WeatherData> weatherData(DateTime date1, DateTime date2)
{
List<WeatherData> list = new List<WeatherData>();

         if (date1 <= date2)
         {

             XElement xml = XElement.Load(PATH);

             var query = from data in xml.Elements("measurment")
                         where DateTime.Compare(DateTime.Parse(data.Element("date").Value), date1) >= 0 && DateTime.Compare(DateTime.Parse(data.Element("date").Value), date2) <= 0      //DateTime.Parse(xml.Element("date").Value) >= date1 && DateTime.Parse(xml.Element("date").Value) <= date2
                         select data;

             if (query.Any())
             {
                 foreach (var w in query)
                 {
                     WeatherData tmp = new WeatherData();
                     tmp.Temperature = w.Element("temperature").Value;
                     tmp.Humidity = w.Element("humidity").Value;
                     list.Add(tmp);
                 }
             }
             else
             {
                 WeatherData errorData = new WeatherData();
                 errorData.Error = "Its wrong.";
                 list.Add(errorData);
             }
         }
         else
         {
             WeatherData error = new WeatherData();
             error.Error = "No";
             list.Add(error);
         }

         return list;
     }


My Querry does not have any funcionality somehow and I don't know what to do...

dotnet-csharp
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

By the way, if you need my Interface I wanted to implement into the Service:
Maybe my error lies there

[ServiceContract]
public interface IService1
{

     [OperationContract]
     List<WeatherData> weatherData(DateTime date1, DateTime date2);

     // TODO: Hier Dienstvorgänge hinzufügen
 }


 // Verwenden Sie einen Datenvertrag, wie im folgenden Beispiel dargestellt, um Dienstvorgängen zusammengesetzte Typen hinzuzufügen.
 [DataContract]
 public class WeatherData
 {
     [DataMember]
     public string Error { get; set; }
     [DataMember]
     public string Temperature { get; set; }
     [DataMember]
     public string Humidity { get; set; }
 }
0 Votes 0 ·

My program should pick everything in a range of 2 dates from an xml-file (Humidity, Temperature, ...). I did a relative path and I tried the querry, but the debugger always stops at my query and tells me that something is wrong.

0 Votes 0 ·

@MikoKage
What is the exception displayed by the compiler, could you please show some messages?
Could you please provide some example xml data so that we can reproduce your problem quickly?

0 Votes 0 ·
SuchenderFarn17-4440 avatar image
1 Vote"
SuchenderFarn17-4440 answered MikoKage commented

Add this to your Code:
101379-grafik.png



grafik.png (59.1 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Okay I'm trying it out tomorrow! Thanks in advance

0 Votes 0 ·
alf-4247 avatar image
0 Votes"
alf-4247 answered

A standard LINQ Query consists of the following

var query = from <name> in <yourLoadedXMLFile>.Elements("YourXMLObject")
where <name>.Element("AnyPropertyOfYourObject").Value.Contains(yourVariableYouWantToCompare)
select <name>.Element("ThePropertyYouWantToGetSpittenOut");

Here you want something that is written in that XML File that you want get out, f.e. you want your Temperatures out for the date you want, you compare for the date and then give out any temperatures for that date you just searched

Kind regards Alf

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.