c# - 使用 Linq to sql 将 List<String> 插入到 ntext 字段

标签 c# database linq list

我有这门课:

public class Course
{
    public String Name { set; get; }
    public int Code { set; get; }
    public List<String> PreRequireCources = new List<String>();
}

PreRequireCources 列表由这样的列表框填充:

Course cu = new Course();
cu.Name = txtName.Text;
cu.Code = Convert.ToInt32(txtCode.Text);
cu.PreRequireCources= lstPreRequirsist.Items
    .Cast<string>().ToList();

这是我的类(class)表:

enter image description here

如果我这样做,我会得到一个错误,因为 PreRequireCources 是一个 List 但数据库列是一个 ntext:

var db = new LinqDataContext();
db.Cources.InsertOnSubmit(cc);

那么如何将这个列表保存到我的数据库中呢?有没有更好的方法可以将此列表保存到每个学生的数据库中?
谢谢。

最佳答案

您正在尝试将值列表插入到需要单个文本值的列中。所以你可以将列表中的所有字符串连接成一个字符串

 cu.PreRequireCources= String.join(", ",lstPreRequirsist.Items
.Cast<string>().ToList());

用您的分隔符替换“,”。

关于c# - 使用 Linq to sql 将 List<String> 插入到 ntext 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36327859/

相关文章:

c# - MVC Abstract Base Controller Override modelbinding 参数类型

c# - SHA1(c#) - .Net 3 和 .Net Core 的不同结果

android - 如何在android中存储具有可变数量属性的数据

c++ - 在 C++ 程序中包含大量文本文件

c# - 有没有办法将 lambda 表达式作为变量或参数传递?

c# - log4net - 自定义属性日志记录

mysql - 帖子的喜欢和不喜欢系统

c# - 如何将动态 'where' 子句添加到 linq 查询?

linq - FluentValidation for WP7 - 验证简单类型​​(字符串)

c# - 我可以使用无限范围并对其进行操作吗?