English 中文(简体)
如何将值添加到列表 <valuepair> 对象
原标题:How to add Values to list<valuepair> object
  • 时间:2012-05-25 13:55:58
  •  标签:
  • c#
  • asp.net

我有 List< ValuePair> 对象: listuserRoleValuePair 。 我必须添加 ixUser name , 以作为我每个圆环列表中的数值对。 我如何才能添加这个?

List<ValuePair> listUserRoleValuePair = new List<ValuePair>();
var ixUserList= _mapper1.FindUserRoleLike(sName);
User result = null;

foreach (var ixUser in ixUserList)
{
    result = new UserMapper(connection).FindById(ixUser);
    var name = result.SFirstName + " " + result.SLastName;

    //listUserRoleValuePair.Add(ixUser);
    // listUserRoleValuePair.Add(    
}

我的价值观对等类如下:

public class ValuePair
{
    private string _index;
    private string _sName;

    public ValuePair(string index, string sName)
    {
        _index = index;
        _sName = sName;
    }

    public string Index
    {
        get { return _index; }
        set { _index = value; }
    }

    public string SName
    {
        get { return _sName; }
        set { _sName = value; }
    }
}
最佳回答

提供 ixUser 是 索引字符串, 请使用此代码 :

List<ValuePair> listUserRoleValuePair = new List<ValuePair>();
var ixUserList= _mapper1.FindUserRoleLike(sName);
User result = null;

foreach (var ixUser in ixUserList)
{
    result = new UserMapper(connection).FindById(ixUser);
    var name = result.SFirstName + " " + result.SLastName;
    listUserRoleValuePair.Add(new ValuePair(ixUser, name));
}
问题回答

您可以使用 KeyValuePair 类 :

List<KeyValuePair<string,string>> listUserRoleValuePair = new List<KeyValuePair<string,string>>();

然后:

listUserRoleValuePair.Add(new KeyValuePair<string,string>(name, ixUser);

也可以使用词典代替列表。

listUserRoleValuePair.Add(new ValuePair(index, name));

Im, 不清楚您对象的索引值是什么, 但不管它是什么, 只要在上行替换它

在您的情况下,只能添加支持子对象类型的项目,您可以使用其中之一。

collection.Add(new ValuePair(index, Name))

or
collection.Add(new ValuePair(){Index = index,SName= Name});

或非常简单的创建对象和通过

Valuepair valpair = new ValuePair();
valpair.Index = index;
valpair.SName = name;

collection.Add(valpair);

试试这个

var pair = new ValuePair();
// somehow intialize pair
listUserRoleValuePair.Add(pair);
listUserRoleValuePair.Add(new ValuePair(ixUser, name));




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签