LIST< T>将其分配给字符串 [英] LIST<T> assign it to string
本文介绍了LIST< T>将其分配给字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
全部好,
列表<经销商> dealer = new List< Dealer>()
{
new Dealer {Location = new Location(){Lat = 1.1,Long = 1.2},DealerCode =Dealer1,Address = new Address(){ City =CITY1,State =STATE1}},
new Dealer {Location = new Location(){Lat = 2.1,Long = 2.2},DealerCode =Dealer2,Address = new Address(){ City =CITY2,State =STATE2}},
new Dealer {Location = new Location(){Lat = 3.1,Long = 3.2},DealerCode =Dealer3,Address = new Address(){ City =CITY3,State =STATE3}}
};
我希望这个列表被分配到一个字符串中,我对此通用不太好。
你能帮帮我吗?
问候
Magesh
我尝试了什么:
我试图分配列表< t>值到字符串中。但是我不喜欢这个通用你能帮忙
解决方案
最简单的方法是在Dealer类中创建一个返回字符串版本的方法 - 和显而易见的候选人是ToString:
public override string ToString()
{
return string .Join( ,Location.Lat.ToString() ,Location.Long.ToString(),DealerCode,
Address.City,Address.State);
}然后您就可以在列表中使用它:
List< string> strings = dealers.Select(d = > d.ToString())。ToList();
或者对于单个字符串:
string str = string .Join(< span class =code-string> ,dealer.Select(d = > d.ToString()+ \ n));
HI All,
List<Dealer> dealers = new List<Dealer>() { new Dealer{ Location =new Location() {Lat=1.1, Long=1.2 }, DealerCode="Dealer1", Address=new Address() { City="CITY1", State="STATE1" } }, new Dealer{ Location =new Location() {Lat=2.1, Long=2.2 }, DealerCode="Dealer2", Address=new Address() { City="CITY2", State="STATE2" } }, new Dealer{ Location =new Location() {Lat=3.1, Long=3.2 }, DealerCode="Dealer3", Address=new Address() { City="CITY3", State="STATE3" } } };
I want this list to be assigned into a string, im not good in this generic.
Can you help me?
regards
Magesh
What I have tried:
I tried to assign the list<t> values in to a string. but im not good in this generic could you please help
解决方案
The easiest way to do it is to create a method in your Dealer class that returns a string version - and the obvious candidate is ToString:
public override string ToString() { return string.Join("", Location.Lat.ToString(), Location.Long.ToString(), DealerCode, Address.City, Address.State); }You can then use that in your List like this:
List<string> strings = dealers.Select(d => d.ToString()).ToList();
Or for a single string:
string str = string.Join("", dealers.Select(d => d.ToString() + "\n"));
这篇关于LIST< T>将其分配给字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文