I have a template TimeSpan in MVC.
View
@model TimeSpan?
@{
var id = "id" + Guid.NewGuid().ToString().Substring(0, 5);
string format = (string)(this.ViewData["format"] ?? @"hh\:mm\:ss");
IEnumerable<SelectListItem> listValues;
if (this.Model.HasValue)
{
listValues = from x in Enumerable.Range(0, 96)
.Select(x => new TimeSpan(9000000000 * x))
.Select(x => new SelectListItem {Selected = true, Value = x.ToString(),
Text = x.ToString(format) })
}
else
{
listValues = from x in Enumerable.Range(0, 96)
select new SelectListItem { Value = x.ToString(),
Text = x.ToString(format) };
}
}
<div class="field-small">
@Html.DropDownListFor(x => x, listValues, new { id = id})
</div>
<script type="text/javascript"">
$("#@id")
.turnAutoComplete();
</script>
but have exception
The type of the expression in the select clause is incorrect. Type inference failed in the call to 'Select'.
and
A query body must end with a select clause or a group clause
Line error
listValues = from x in Enumerable.Range(0, 96)
.Select(x => new TimeSpan(9000000000 * x))
.Select(x => new SelectListItem { Selected = true, Value = x.ToString(),
Text = x.ToString(format) })
I do not know about identifying what's wrong with my line
You're trying to mix query expression syntax with regular method calls, and you've ended up with something that isn't a complete query expression. You could use:
listValues = from x in Enumerable.Range(0, 96)
let ts = new TimeSpan(9000000000 * x)
select new SelectListItem {
Selected = true,
Value = ts.ToString(),
Text = ts.ToString(format)
};
Or just:
listValues = Enumerable.Range(0, 96)
.Select(x => new TimeSpan(9000000000 * x))
.Select(x => new SelectListItem {
Selected = true,
Value = x.ToString(),
Text = x.ToString(format)
});
See more on this question at Stackoverflow