C# 使用automapper映射时,是否可以在对象列表中添加行号?
我想在对象列表中添加行号。C# 使用automapper映射时,是否可以在对象列表中添加行号?,c#,asp.net-mvc,entity-framework,automapper,C#,Asp.net Mvc,Entity Framework,Automapper,我想在对象列表中添加行号。 这是我现在做的方法,但一定有更好的方法 映射配置文件 public class VendorEnquiryDM_TO_VM:Profile { 公共供应商查询到虚拟机() { CreateMap(); } } 公共类供应商查询:配置文件 { 公共供应商查询 { CreateMap().ReverseMap(); } } 寄存器配置文件 cfg.AddProfile(); AddProfile(); 我就是这样添加sno的。 alldata=Mapper.Ma
这是我现在做的方法,但一定有更好的方法
映射配置文件
public class VendorEnquiryDM_TO_VM:Profile
{
公共供应商查询到虚拟机()
{
CreateMap();
}
}
公共类供应商查询:配置文件
{
公共供应商查询
{
CreateMap().ReverseMap();
}
}
寄存器配置文件
cfg.AddProfile();
AddProfile();
我就是这样添加sno的。
alldata=Mapper.Map(objDAO.getVendorEnquiry());
var\u roles=alldata.Select((t,index)=>new Vendor\u inquiryvm
{
sno=索引+1,
联系人编号=t。联系人编号,
日期=t.DATE,
EMAIL=t.EMAIL,
id=t.id,
FIRST_NAME=t.FIRST_NAME,
wer=t.wer,
asdf=t.asdf
});
由于只有一个序列号。我需要分配所有属性,对于大型模型,这有点让我感到不安,请建议我更好的方法。您可以定义一个静态Id,在创建类时,将其递增一个 下面是您的类代码应该是什么样子
public class Test
{
private static int mId = 0;
public Test()
{
mId = mId +1;
}
public int Id
{
get{ return mId;}
}
}
为了对像List
这样的集合使用相同的想法,我应用了一些修改,下面是您可以做的
public class Test
{
private static int mIndex = 0; // this parameter will be incremented for each new Test
private int mId =0; // this parameter will hold the last incremented value
public Test()
{
mId = ++mIndex; // mIndex++ if you want to start with 0
}
public int Id
{
get{ return mId;}
}
}
希望这将帮助您它会在测试类对象集合中返回ItemIndex+1吗?@BrBhardwaj是的,检查演示此解决方案中您的问题到底在哪里?@BrBhardwaj我修改了解决方案,根据您的第一条评论使用集合,需要15个信誉点来标记为答案,谢谢。
alldata = Mapper.Map<IEnumerable<Vendor_EnquiryVM>>(objDAO.getVendorEnquiry());
var _roles = alldata.Select((t, index) => new Vendor_EnquiryVM
{
sno = index + 1,
CONTACT_NO=t.CONTACT_NO,
DATE=t.DATE,
EMAIL=t.EMAIL,
id=t.id,
FIRST_NAME=t.FIRST_NAME,
wer=t.wer,
asdf=t.asdf
});
public class Test
{
private static int mId = 0;
public Test()
{
mId = mId +1;
}
public int Id
{
get{ return mId;}
}
}
public class Test
{
private static int mIndex = 0; // this parameter will be incremented for each new Test
private int mId =0; // this parameter will hold the last incremented value
public Test()
{
mId = ++mIndex; // mIndex++ if you want to start with 0
}
public int Id
{
get{ return mId;}
}
}