![](/img/trans.png)
[英]Adding detached entities to a 1-many relationship in EF 4.1 Code First
[英]How to join columns or properties of entities that belongs as many relationship to another entity in .NET EF Code First
我正在开发一个 ASP.NET MVC 项目。 在我的项目中,我使用实体框架代码优先方法。 但我对此有问题。
这是项目类
class Item{
public int Id { get; set; }
.
.
.
public virtual ICollection<Category> Categories { get; set; }
}
这是类别类
public class Category{
public int Id{ get; set; }
public int Name { get; set; }
public virtual ICollection<Item> Items { get; set; }
}
正如你在上面看到的,它们是多对多的关系。 我想要做的是如下。
var item = db.find(2); //I retrieve a single item
var item.Categories; // this is how I get all categories of that item
item.Categories.Select(x=>x.Name).Join(?); // I want to retrieve all category names of that item as CSV value.
我怎样才能做到这一点?
由于您已经为这个项目的所有类别,我假设您正在寻找如何将字符串列表转换为逗号分隔的字符串。 是吗? 如果是这样,那么您可以执行此处显示的操作: Converting a generic list to a CSV string
IEnumerable<T> myList;
string csv = String.Join(",", myList.Select(x => x.ToString()).ToArray());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.