我有两个表/实体
public class Attribute
{
public int Id { get; set; }
public string Name { get; set; }
public string Details { get; set; }
public bool Inactive { get; set; }
public virtual ICollection<Item> Items{ get; set; }
}
public class Item
{
public int Id { get; set; }
public DateTime DateCreated { get; set; }
public DateTime? DateModified { get; set; }
public virtual ICollection<Attribute> Attributes { get; set; }
}
我试图找出Linq查询来检索项目ID的所有属性名称,其中属性Inactive status设置为false。属性计数可以是0到20。
答案 0 :(得分:1)
以下内容应该为您提供具有指定ID的项目的所有属性名称。
var selectedId = 1; // The item ID you are looking for
var attrNames = items
.Where(i => i.Id == selectedId)
.SelectMany(x => x.Attributes)
.Where(a => !a.Inactive)
.Select(a => a.Name);