C# 使用 Linq 返回逗号分隔的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18991360/
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 13:49:37  来源:igfitidea点击:

Using Linq to return a Comma separated string

c#linq

提问by Mako

I have a class in my application

我的申请中有一个课程

public class ProductInfo
{
  public int ProductId {get;set;}
  public int ProductType{get;set;}
}

I want to write a linq query which can return me a list of ProductIds in a comma separated format where ProductType is equal to certain number ?

我想编写一个 linq 查询,它可以以逗号分隔的格式返回一个 ProductIds 列表,其中 ProductType 等于某个数字?

I tried using string.join with my Linq statement but it didn't seem to work.

我尝试在我的 Linq 语句中使用 string.join,但它似乎不起作用。

采纳答案by King King

var s = string.Join(",", products.Where(p => p.ProductType == someType)
                                 .Select(p => p.ProductId.ToString()));