简体   繁体   English

将IEnumerable <T>转换为string []

[英]Convert IEnumerable<T> to string[]

i have an entity called Product 我有一个名为Product的实体

class Product
{
     public Id { get; set; }
     public Name { get; set; }
}

and i have a list of all products: 我有一个所有产品的清单:

IEnumerable<Product> products = _productRepository.GetAll()

i want to get an array of strings from this list of products this array will contains the product Id + Product Name, so when i try to cast it using the following code: 我想从这个产品列表中获取一个字符串数组,这个数组将包含产品Id +产品名称,所以当我尝试使用以下代码强制转换它时:

string[] s = products.Cast<string>().ToArray();

i got the following exception: 我得到以下例外:

Unable to cast object of type 'Product' to type 'System.String'

the exception really makes alot fo scence, so if i had a method 这种例外真的会让人觉得很多,所以如果我有一个方法

string ProductToString(Product p)
{
    return p.Name;
}

or an override to ToString() for the product object so how i can use this method to get the list of string[] from IEnumerable ? 或者对产品对象的ToString()重写,以便我如何使用此方法从IEnumerable获取string []列表?

Well, given that method you can use 1 : 那么,鉴于这种方法你可以使用1

string[] s = products.Select<string>(ProductToString).ToArray();

However, it would be more idiomatic to do this without a separate method, usually, using a lambda expression: 但是,如果没有单独的方法,通常使用lambda表达式来执行此操作会更加惯用:

// Matches ProductToString, but not your description
string[] s = products.Select(p => p.Name).ToArray();

I'd only use a separate method if it was going to be called from various places (ensuring consistency) or did a lot of work. 如果要从各个地方调用(确保一致性)或做了很多工作,我只会使用一个单独的方法。

EDIT: I've just noticed that your description (wanting ID + name) doesn't actually match the ProductToString method you've given (which just gives the name). 编辑:我刚刚注意到你的描述 (想要ID +名称)实际上并不匹配你给出的ProductToString方法(只提供名称)。 For the ID + name I'd use: 对于我使用的ID +名称:

string[] s = products.Select(p => p.ID + " " + p.Name).ToArray();

or 要么

string[] s = products.Select(p => string.Format("{0} {1}", p.ID, p.Name))
                     .ToArray();

Or you could just change your ProductToString method, of course. 或者你可以改变你的ProductToString方法。

Alternatively, you could override ToString() in Product , if this is usually how you want to convert a Product to a string . 或者,您可以覆盖Product ToString() ,如果这通常是您希望将Product转换为string You could then either use a method group conversion or a lambda expression to call ToString . 然后,您可以使用方法组转换或lambda表达式来调用ToString


1 It's possible that you don't need to specify the type argument explicitly - that: 1您可能不需要明确指定类型参数 - 即:

string[] s = products.Select(ProductToString).ToArray();

will work fine - the rules for type inference and method group conversions always confuse me and the compiler behaviour has changed slightly over time. 将正常工作 - 类型推断和方法组转换的规则总是让我困惑,编译器行为随着时间的推移略有变化。 A quick test just now looks like it does work, but there could be subtleties in slightly different situations. 刚刚进行的快速测试看起来确实有效,但在略有不同的情况下可能存在微妙之处。

string[] s = products.Select(p => p.Name).ToArray();

或者,如果您需要Id +名称:

string[] s = products.Select(p => p.Id + ' ' + p.Name).ToArray();

使用

string[] s = (from p in products select p.Id.ToString() + " " + p.Name.ToString()).ToArray();

This worked for me: 这对我有用:

String.Join(";", mi_customer.Select(a => a.Id).Cast<string>().ToArray());

Where mi_customer must be your object, in my case is a table. mi_customer必须是你的对象,在我的情况下是一个表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM