如何为列表指定值< string>在模型中 [英] How to assign a value to a list<string> inside a model

查看:60
本文介绍了如何为列表指定值< string>在模型中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的模型就像是

my model is like is

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; }
  }




I Need to add assign value to the above model but the proble is adding the value to the list of string that is inside the model, List<string> FilePathList  i need something like this,







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);





我的尝试:



我试图通过创建模型的obj来添加值,并尝试分配值但无法添加值。



What I have tried:

I have try to add the value by creating a obj of the model and try to assign a value but couldn't add the value.

推荐答案

另一种方法,你可以动态添加/删除项目





Another way, which you can add/remove items dynamically


        List<string> lstPathList = new List<string> ();
        lstPathList.Add("path1");
        lstPathList.Add("path2");
        lstPathList.Add("path3");
       
var obj = new MovieListModel()
                   {
                       FileName = fi.Name,
                       FilePathList = lstPathList, // 
                       Count = 1,
                       Extension = fi.Extension,
                       Size = Helper.FormatBytes(fi.Length),
                       CreatedTime = fi.CreationTime,
                       ModifiedTime = File.GetLastWriteTime(item)
                   };
                   movieList.Add(obj);


var obj = new MovieListModel()
                   {
                       FileName = fi.Name,
                       FilePathList = new List<string>{"string 1", "string 2"},
                       Count = 1,
                       Extension = fi.Extension,
                       Size = Helper.FormatBytes(fi.Length),
                       CreatedTime = fi.CreationTime,
                       ModifiedTime = File.GetLastWriteTime(item)
                   };


另一个选项是你可以编写返回文件路径的linq查询



another option is you can write linq query that returns file path also

var obj = new MovieListModel()
           {
               FileName = fi.Name,
               FilePathList =(
                             from d in file
                             select d.FilePath
                             ),
               Count = 1,
               Extension = fi.Extension,
               Size = Helper.FormatBytes(fi.Length),
               CreatedTime = fi.CreationTime,
               ModifiedTime = File.GetLastWriteTime(item)
           };
           movieList.Add(obj);



以上给出的答案也是如此


above given answer in also true


这篇关于如何为列表指定值&lt; string&gt;在模型中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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