How to use Lambda in LINQ select statement
Solution 1
using LINQ query expression
IEnumerable<SelectListItem> stores =
from store in database.Stores
where store.CompanyID == curCompany.ID
select new SelectListItem { Value = store.Name, Text = store.ID };
ViewBag.storeSelector = stores;
or using LINQ extension methods with lambda expressions
IEnumerable<SelectListItem> stores = database.Stores
.Where(store => store.CompanyID == curCompany.ID)
.Select(store => new SelectListItem { Value = store.Name, Text = store.ID });
ViewBag.storeSelector = stores;
Solution 2
Why not just use all Lambda syntax?
database.Stores.Where(s => s.CompanyID == curCompany.ID)
.Select(s => new SelectListItem
{
Value = s.Name,
Text = s.ID
});
Solution 3
You appear to be trying to mix query expression syntax and "normal" lambda expression syntax. You can either use:
IEnumerable<SelectListItem> 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<SelectListItem> 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.
Solution 4
Lambda Expression result
var storesList = context.Stores.Select(x => new { Value= x.name,Text= x.ID }).ToList();
Bill Software Engineer
I am a professional software developer working in San Francisco, USA.
Updated on September 21, 2020Comments
-
Bill Software Engineer over 3 years
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 in Select Clause is Incorrect" error:
IEnumerable<SelectListItem> stores = from store in database.Stores where store.CompanyID == curCompany.ID select (s => new SelectListItem { Value = s.ID, Text = s.Name} ); ViewBag.storeSelector = stores;
What am I doing wrong?
EDIT:
Also, how do I convert Int to String in this situation? The following does not work:
select (s => new SelectListItem { Value = s.ID.ToString(), Text = s.Name} ); select (s => new SelectListItem { Value = s.ID + "", Text = s.Name} );
EDIT 2:
Figure out the Int to String conversion. It is so typical of Microsoft to forget to include an int2string conversion function. Here is the actual workaround everyone is using, with fully working syntax:
select new SelectListItem { Value = SqlFunctions.StringConvert((double)store.ID), Text = store.Name };
To call this situation absurd is an understatement.
-
Bill Software Engineer about 11 yearsCan you please help me convert int to string? There seem to be no way for LINQ to Entity to convert int to string.
-
Russ Cam about 11 years@YongkeBillYu You mean like for example
5.ToString()
? -
Russ Cam about 11 yearsCan you create another question and show the code for what you are doing?
-
Kgn-web about 7 years@RussCam, if I am not wrong .
Where(store => store.CompanyID == curCompany.ID)
is of type Func<> lamda expression??? -
Russ Cam about 7 years@Kgn-web yes, a
Func<T, bool>
is passed to.Where()
, whereT
is the type inIEnumerable<T>
-
Kgn-web about 7 years@RussCam. Thanks for your inputs. Irrespective of this question, can you please give me scenario where we use Action<> or Predicate<> lamda expression in LINQ. (You may use codepaste.net for sharing the snippet).
-
Russ Cam about 7 years@Kgn-web use
Action<T>
when you want to pass a delegate that doesn't return a value andPredicate<T>
delegate where you need to return a boolean value;Predicate<T>
was introduced in .NET 2.0, and generallyFunc<T, bool>
is more often used, which was introduced in .NET 3.5 IIRC. -
Kgn-web about 7 years@RussCam.Thanks mate :)