-1

我正在使用嵌套选择,似乎我正在做的事情不正确!

我正在尝试检索所有Topics项目。

public class Employee
{
   public int Id{ get; set; }  
   //....other fields....
   //......
   public IList<Topics> Interest { get; set; }     
}

public class Topics
{
   public int Id { get; set; }  ;
   public string Name { get; set; }  ;
   //other fields
}

employeeItems = (from _emp in employees
                   select new Employee
                   {
                      EmpId = _emp.mediaId,
                      EmpName = _emp.mediaType,
                      ......................
                      Interest = (from _emp1 in employees.Interest  //has few rows
                      select new Topic 
                      {
                          Id = _emp1.Topics[0].Id,   //.<int>("id"), <<<ERROR
                          Name = _emp1.Topics[0].Name //["name"] <<<ERROR
                      }).ToList()

               }).ToList();
        }
4

1 回答 1

1
Interest = (from topic in _emp.Interest.SelectMany(i=>i.Topic)  //has few rows
                      select new Topic 
                      {
                          Id = topic.Id,   //.<int>("id"), <<<ERROR
                          Name = topic.Name //["name"] <<<ERROR
                      })

public class Employee
{
   public int Id{ get; set; }  
   //....other fields....
   //......
   public IEnumberable<Topics> Interest { get; set; }     
}

将属性保留为IEnumberable并且不要.ToList()在查询中执行。在我看来,您的数据结构是员工有多个兴趣,每个兴趣都有多个主题,这就是我使用的原因selectmany,但如果我对数据有误,您可以调整它。

于 2013-11-05T00:10:58.417 回答