CodeGo.net>如何在LINQ选择语句中使用Lambda

我正在尝试使用lambda函数选择商店,并将结果转换为SelectListItem,以便可以呈现它。 但是,它将引发“选择子句中的表达式类型不正确”错误:

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;

我究竟做错了什么?

编辑:

另外,在这种情况下如何将Int转换为String? 以下内容不起作用:

select (s => new SelectListItem { Value = s.ID.ToString(), Text = s.Name} );
select (s => new SelectListItem { Value = s.ID + "", Text = s.Name} );

编辑2:

找出从Int到String的转换。 Microsoft非常典型地忘记包含int2string转换函数。 这是每个人都在使用的实际解决方法,语法完全正常:

select new SelectListItem { Value = SqlFunctions.StringConvert((double)store.ID), Text = store.Name };

称这种情况为荒谬是轻描淡写。

Bill asked 2019-11-07T23:26:34Z
5个解决方案
112 votes

使用LINQ查询表达式

 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;

或使用带有lambda表达式的LINQ扩展方法

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

 ViewBag.storeSelector = stores;
Russ Cam answered 2019-11-07T23:26:58Z
18 votes

为什么不只使用所有Lambda语法?

database.Stores.Where(s => s.CompanyID == curCompany.ID)
               .Select(s => new SelectListItem
                   {
                       Value = s.Name,
                       Text = s.ID
                   });
Justin Niessner answered 2019-11-07T23:27:21Z
16 votes

您似乎正在尝试混合使用查询表达式语法和“常规” lambda表达式语法。 您可以使用:

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;

要么:

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

您不能像尝试的那样混合使用两者。

Jon Skeet answered 2019-11-07T23:27:53Z
4 votes

Lambda表达结果

var storesList = context.Stores.Select(x => new { Value= x.name,Text= x.ID }).ToList();
Gurbaksh Singh answered 2019-11-07T23:28:16Z
-1 votes

使用Lambda表达式:

  1. 如果我们没有特定的类来绑定结果:

    List<SelectListItem> stores = (from a in context.Stores select new SelectListItem{ Id = x.id, Name = x.name, City = x.city }).ToList();
    
  2. 如果我们有一个特定的类,那么我们需要将结果与之绑定:

    List<SelectListItem> stores = (from a in context.Stores select new SelectListItem{ Id = x.id, Name = x.name, City = x.city }).ToList();
    

使用简单的LINQ表达式:

  1. 如果我们没有特定的类来绑定结果:

    List<SelectListItem> stores = (from a in context.Stores select new SelectListItem{ Id = x.id, Name = x.name, City = x.city }).ToList();
    
  2. 如果我们有一个特定的类,那么我们需要将结果与之绑定:

    List<SelectListItem> stores = (from a in context.Stores select new SelectListItem{ Id = x.id, Name = x.name, City = x.city }).ToList();
    
Ramakant Shukla answered 2019-11-07T23:29:14Z
translate from https://stackoverflow.com:/questions/15577890/how-to-use-lambda-in-linq-select-statement