使用Linq

时间:2015-10-23 11:45:08

标签: c# linq

我有一个名为start的日期变量,它在查询字符串中传递。使用linq我需要在另一个页面上测试另一个名为StartDate的日期变量是否等于在url中传递的日期变量。

我正在尝试做类似的事情:

test = client.GetEventInstances().Where(e => e.StartDate == start);

然后,我可以使用StartDate的事件进行过滤,该start等于在网址中发送的@model ReportGenerator.WebUI.Models.FieldViewModel @{ var list = @Model.IEnumerableField.GroupBy(item => item.TemplateName).ToList(); } @using (Html.BeginForm("addTemplate", "TemplateField", FormMethod.Post)) { <div class="form-group"> <div class="input-group"> <label class="label label-default">Nazwa szablonu*</label> @Html.TextBoxFor(m => m.field.TemplateName, new { @class = "form-control" }) <br /><br /> </div> </div> <table id="sort" class="table"> <thead> <tr> <th>Nazwa kolumny z BD*</th> <th>Długość rekordu*</th> <th>Nazwa kolumny wyświetlana</th> <th>Format*</th> </tr> </thead> <tbody id="tblsort"> @for (var i = 0; i < Model.listOfFormats(Model.field.TemplateName).ColumnNamesDB.Count; i++) { <tr> <td>@Html.TextBoxFor(m => Model.listOfFormats(Model.field.TemplateName).ColumnNamesDB[i], new { @class = "form-control" })</td> <td>@Html.TextBoxFor(m => Model.listOfFormats(Model.field.TemplateName).LengthColumns[i], new { @class = "form-control" })</td> <td>@Html.TextBoxFor(m => Model.listOfFormats(Model.field.TemplateName).ColumnNamesUser[i], new { @class = "form-control" })</td> <td>@Html.DropDownListFor(m => Model.listOfFormats(Model.field.TemplateName).Formats[i], new SelectList(Model.formatFieldValues, "Value", "Text"), Model.listOfFormats(Model.field.TemplateName).Formats[i], new { @class = "form-control" })</td> </tr> } </tbody> </table> <div id="hiddenHtml" style="display:none;"> @Html.DropDownList("FormatFieldName", new SelectList(Model.formatFieldValues, "Value", "Text"), "Domyślny", new { @class = "form-control" }) </div> <button class="btn btn-success" type="button" id="addRow" style="float: right;"><span class="glyphicon glyphicon-plus" id="addRow"></span></button> <button class="btn btn-success" id="save" style="float: left;">Zapisz</button> <br /><br /><br /> <div> <p style="float: right;">* - Pole oznaczone gwiązdką są obowiązkowe!</p> </div> <br /> <br /> @Html.ValidationSummary() <h1 style="color:@ViewBag.color">@ViewBag.Info</h1> } var

2 个答案:

答案 0 :(得分:0)

您需要将字符串日期转换为DateTime: -

DateTime startDate  = DateTime.Parse(start);
test = client.GetEventInstances().Where(e => e.StartDate.Date == startDate.Date);

显然,您必须检查日期是否已被解析。

答案 1 :(得分:0)

您应该使用DateTime.TryParse()

DateTime startDateTime;
if (DateTime.TryParse(start, out startDateTime))
    client.GetEventInstances().Where(e => e.StartDate.Date == startDateTime.Date);
else
    /* query can't be parsed, do whatever you need to do */