How to use Lambda in LINQ select statement

前端 未结 5 2008
孤独总比滥情好
孤独总比滥情好 2020-12-04 08:01

I am trying to select stores using a lambda function and converting the result to a SelectListItem so I can render it. However it is throwing a \"Type of Expression

5条回答
  •  误落风尘
    2020-12-04 08:24

    You appear to be trying to mix query expression syntax and "normal" lambda expression syntax. You can either use:

    IEnumerable stores =
            from store in database.Stores
            where store.CompanyID == curCompany.ID
            select new SelectListItem { Value = store.Name, Text = store.ID};
    ViewBag.storeSelector = stores;
    

    Or:

    IEnumerable stores = database.Stores
            .Where(store => store.CompanyID == curCompany.ID)
            .Select(s => new SelectListItem { Value = s.Name, Text = s.ID});
    ViewBag.storeSelector = stores;
    

    You can't mix the two like you're trying to.

提交回复
热议问题