0

我有一个名为 States 的表,我正在使用 linq 查询在下拉列表中显示状态值现在我想在 id 0 的第一个位置插入选择。我正在使用以下查询来显示下拉列表:

var str = (from li in dbContext.states

               where li.state >= 1
               select new
               {

                   a=li.state

               }).ToArray();

上面的查询给出了我的 DB 中存在的所有值的平均值。我如何首先插入选择一个?请任何人提出任何方法或语法。谢谢

4

2 回答 2

0

您必须在列表的开头添加一个额外的项目。

var str = (from li in dbContext.states

               where li.state >= 1
               select new
               {

                   a=li.state

               }).ToList().Insert(0, "selectone");
于 2012-06-18T10:52:56.253 回答
0

See MajoBs answer.

Although if you're using the @Html.DropDownListFor() Helper, you could also specify the Id 0 row using the optionLabelArgument:

public static MvcHtmlString DropDownListFor<TModel, TProperty>(
    this HtmlHelper<TModel> htmlHelper,
    Expression<Func<TModel, TProperty>> expression,
    IEnumerable<SelectListItem> selectList,
    string optionLabel
)

Update:

I think the reason MajoB's example doesn't work is because you are using an anonymous type in the Linq query, but define a string in the .Insert() statement.

I believe it is not possible to reuse the same anonymous type that was used in the LINQ query in the .Insert() statement.

So you would have to use a regular type.

(from x in 
    (from li in dContext.states
    where li.state >= 0
        select new {
            value = li.state,
            text = li.something
        }).AsEnumerable()
    select new SelectListeItem {
        Value = x.value.ToString()
        Text = x.text
    }).ToList().Insert(0, new SelectListItem() { .. });

The nested query is necessary because the .ToString() can't be translated to sql. The .AsEnumerable() call created an in-memory list where .ToString will work on.

I am actually using a Extension method like this in our code base. But it requires LinqKit to work.

public static IList<SelectListItem> ToSelectList<T>(this IQueryable<T> query
        , Expression<Func<T, long>> value
        , Expression<Func<T, string>> text
        , string nullText = null)
    {
        var result = (from x in
                          (from y in query.AsExpandable()
                           select new
                           {
                               Value = value.Invoke(y),
                               Text = text.Invoke(y),
                           }).AsEnumerable()
                      select new SelectListItem
                      {
                          Value = x.Value.ToString(),
                          Text = x.Text.ToString()
                      }).ToList();

        AddNullText(nullText, result);
        return result;
    }

private static void AddNullText(string nullText, List<SelectListItem> result)
    {
        if (nullText != null)
        {
            result.Insert(0, new SelectListItem()
            {
                Value = "",
                Text = nullText,
            });
        }
    }

(+ the same Extension method with Expression<Func<T, string>>, etc.. as second parameter.)

于 2012-06-18T10:56:43.950 回答