将通用列表类型转换为字符串列表 [英] Convert Generic list type to string list

查看:88
本文介绍了将通用列表类型转换为字符串列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现在将返回通用列表类型集合的方法,其中我返回的是要返回的对象集合的集合,但遇到无法将通用列表类型转换为对象类型集合的错误,所以请告诉我 如何将通用列表转换为字符串列表类型集合?

I have method which will return Generic list type collection now inside that i have returning collection which is of Object Collection that is to be returned but getting error that cannot convert Generic list type to object type collection so plz tell me how do i convert Generic list to string list type collection?

 

 

推荐答案

如果您可以使用LINQ,那将非常容易.

If you can use LINQ it will be really easy.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SampleConvertion
{
    class SampleConvertion
    {

        public class Student
        {

            public int StudentId { get; set; }
            public string StudentName { get; set; }
        }

        public List<string> ConvertStudentListToStringList(List<Student> list)
        {

            //  You need to specify the string field. i.e. StudentName in this sample.
            return list.Select(f => f.StudentName).ToList();
        }
    }
}

如果您的对象可转换为字符串,则可以在内部使用任意类型的转换 (f => f.StudentName)块.

If your object is convertable to string you can use any type of casting that you like inside the (f=>f.StudentName) block.

干杯

Barsham


这篇关于将通用列表类型转换为字符串列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆