我的模型就像是
public class MovieListModel
{
public int Id { get; set; }
public List<string> FilePathList { get; set; }
public string FileName { get; set; }
public string Extension { get; set; }
public string Size { get; set; }
public DateTime CreatedTime { get; set; }
public DateTime ModifiedTime { get; set; }
}
我需要为上述模型添加赋值,但问题是将值添加到模型内的字符串列表List FilePathList中。我需要这样的东西,
var obj = new MovieListModel()
{
FileName = fi.Name,
FilePathList = , // Need to add value here.
Count = 1,
Extension = fi.Extension,
Size = Helper.FormatBytes(fi.Length),
CreatedTime = fi.CreationTime,
ModifiedTime = File.GetLastWriteTime(item)
};
movieList.Add(obj);
答案 0 :(得分:4)
这是一种方式:
var obj = new MovieListModel()
{
FileName = fi.Name,
FilePathList = new List<string>() { "Value" }, // Need to add value here.
Count = 1,
Extension = fi.Extension,
Size = Helper.FormatBytes(fi.Length),
CreatedTime = fi.CreationTime,
ModifiedTime = File.GetLastWriteTime(item)
};
movieList.Add(obj);
根据您准确添加的内容,可能有更好的方式。
答案 1 :(得分:2)
var obj = new MovieListModel()
{
FileName = fi.Name,
FilePathList = new List<string> {
// Add value here
},
Count = 1,
Extension = fi.Extension,
Size = Helper.FormatBytes(fi.Length),
CreatedTime = fi.CreationTime,
ModifiedTime = File.GetLastWriteTime(item)
};
答案 2 :(得分:0)
这是你需要的吗
var obj = new MovieListModel()
{
FileName = fi.Name,
FilePathList = new List<string>(){ obj, whatever...},
Count = 1,
Extension = fi.Extension,
Size = Helper.FormatBytes(fi.Length),
CreatedTime = fi.CreationTime,
ModifiedTime = File.GetLastWriteTime(item)
};
答案 3 :(得分:0)
试试这样。通过创建一个函数,您将能够向其传递任意数量的参数,并获得所需的输出 -
var obj = new MovieListModel()
{
FileName = fi.Name,
FilePathList = MapMyStrings(fi.prop1, fi.prop2),
Count = 1,
Extension = fi.Extension,
Size = Helper.FormatBytes(fi.Length),
CreatedTime = fi.CreationTime,
ModifiedTime = File.GetLastWriteTime(item)
};
List<string> MapMyStrings(params string[] objs){
if(!objs.Length > 0) return null;
return objs.ToList();
}
答案 4 :(得分:0)
我知道上面的答案会回答你的问题,但我只是想告诉你这个,以防你想要放在那个列表中的是不是文字字符串(所以如果你需要转换)
var obj = new MovieListModel()
{
FileName = fi.Name,
FilePathList = new List<string> { "Test", "Test1", fi.CreatedTime.ToString() /* use of .ToString() to convert other objects to string*/} /* and so on and so on */,
Count = 1,
Extension = fi.Extension,
Size = Helper.FormatBytes(fi.Length),
CreatedTime = fi.CreationTime,
ModifiedTime = File.GetLastWriteTime(item)
};
movieList.Add(obj);
希望这有帮助!