C# 如何在 LINQ 选择语句中使用 Lambda

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15577890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 17:16:08  来源:igfitidea点击:

How to use Lambda in LINQ select statement

c#asp.net-mvclinqlambda

提问by Bill Software Engineer

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:

我正在尝试使用 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;

What am I doing wrong?

我究竟做错了什么?

EDIT:

编辑:

Also, how do I convert Int to String in this situation? The following does not work:

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

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

EDIT 2:

编辑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:

找出 Int 到 String 的转换。微软忘记包含一个 int2string 转换函数是很典型的。这是每个人都在使用的实际解决方法,具有完全有效的语法:

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

To call this situation absurd is an understatement.

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

采纳答案by Russ Cam

using LINQ query expression

使用 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;

or using LINQ extension methods with lambda expressions

或将 LINQ 扩展方法与 lambda 表达式一起使用

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

 ViewBag.storeSelector = stores;

回答by Jon Skeet

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

您似乎试图混合查询表达式语法和“正常” 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;

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.

你不能像你想的那样混合两者。

回答by Justin Niessner

Why not just use all Lambda syntax?

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

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

回答by Ramakant Shukla

Using Lambda expressions:

使用 Lambda 表达式:

  1. If we don't have a specific class to bind the result:

     var stores = context.Stores.Select(x => new { x.id, x.name, x.city }).ToList();
    
  2. If we have a specific class then we need to bind the result with it:

    List<SelectListItem> stores = context.Stores.Select(x => new SelectListItem { Id = x.id, Name = x.name, City = x.city }).ToList();
    
  1. 如果我们没有特定的类来绑定结果:

     var stores = context.Stores.Select(x => new { x.id, x.name, x.city }).ToList();
    
  2. 如果我们有一个特定的类,那么我们需要将结果与它绑定:

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

Using simple LINQ expressions:

使用简单的 LINQ 表达式:

  1. If we don't have a specific class to bind the result:

    var stores = (from a in context.Stores select new { x.id, x.name, x.city }).ToList();
    
  2. If we have a specific class then we need to bind the result with it:

    List<SelectListItem> stores = (from a in context.Stores select new SelectListItem{ Id = x.id, Name = x.name, City = x.city }).ToList();
    
  1. 如果我们没有特定的类来绑定结果:

    var stores = (from a in context.Stores select new { x.id, x.name, 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();
    

回答by Gurbaksh Singh

Lambda Expression result

Lambda 表达式结果

var storesList = context.Stores.Select(x => new { Value= x.name,Text= x.ID }).ToList();