简体   繁体   中英

Type inference failure with LINQ select

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)
                 });

Once you start using the query expression syntax with "from", you must then also use "select" instead of .Select().

You can do what you wanted (calculating the TimeSpan once then using it twice) with a "let"

  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)
                 });

);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM