添加其他查询以返回(查看)

时间:2014-06-09 14:05:28

标签: sql linq asp.net-mvc-5

我有一个 ViewModel ,我现在返回一个查询,如果电影有员工,他们有角色,那么电影,演员和角色都会被返回。< / p>

现在我想为此添加一个额外的查询,使同一电影中所有员工的string(或其他最佳效果)。所以,如果有三个人在同一部电影中,我想要一个列表,以便我可以在我的视图中调用它。

我当前的查询/ 控制器

        var parameter = Int32.Parse(Filter);
        var queryString =
        from m in db.Movies

        join me in db.MovieEmployees
        on m.ID equals me.movieID

        join e in db.Employees
        on me.employeeID equals e.ID

        join r in db.Roles
        on me.roleID equals r.ID

        where (parameter == 1 && m.Name.Contains(searchString)) || 
              (parameter == 2 && e.Name.Contains(searchString)) || 
              (parameter == 3 && r.RoleType.Contains(searchString))

        select new StarringViewModel { employeeID = e.ID, movieID = m.ID, roleID = r.ID,
               movieName = m.Name, movieDescription = m.Description, 
               movieReleaseDate = m.ReleaseDate, employeeBirthdate = e.Birthday, 
               employeeName = e.Name, Role = r.RoleType };

            return View(queryString.Distinct().ToList().OrderBy(x => x.movieName));

它看起来像这样

enter image description here 看看我是如何在&#34;史上最伟大的电影&#34;中找到两名员工,我想在&#34;主演&#34;中找回员工名单(employeeList)。我在电影下面的字段

供参考,这是我的Model,ViewModel和DB布局。

模型

public class StarringViewModel
    {
        public int movieID { get; set; }
        public int roleID { get; set; }
        public int employeeID { get; set; }
        public string movieName { get; set; }
        public string movieDescription { get; set; }
        public DateTime? movieReleaseDate { get; set; }
        public string Role { get; set; }
        public string employeeName { get; set; }
        public DateTime employeeBirthdate { get; set; }
        public string employeeList { get; set; } // <-- I want this to be the list of actors in same movies
    }

查看

<table class="table table-striped table-hover table-responsive table-condensed">
        <tr>
            <th>
                <h3 style="font-size: x-large; font-weight: bolder">Movie Name</h3>
            </th>
            <th>
                <h3 style="font-size: x-large; font-weight: bolder">Release Date</h3>
            </th>
            <th>
                <h3 style="font-size: x-large; font-weight: bolder">Employee</h3>
            </th>
            <th>
                <h3 style="font-size: x-large; font-weight: bolder">@Html.DisplayNameFor(model => model.Role)</h3>
            </th>
            <th>
                @using (Html.BeginForm("Index", "Starring"))
                {
                    <div class="dropdown">
                        <select class="btn btn-group-lg btn-default col-md-4" style="margin-top: 15px; height: 36px; opacity: 1" data-toggle="dropdown" name="Filter">
                            <option value="0" disabled selected>Filter By...</option>
                            <option value="1">Movie Name</option>
                            <option value="2">Actor Name</option>
                            <option value="3">Role</option>
                        </select>
                    </div>

    <input type="text" name="searchString" class="col-md-6" style="margin-top: 16px; text-align:center; height:35px; font-size:20px" placeholder="enter text" />
    <button type="submit" class="btn btn-group-lg btn-primary col-md-2 glyphicon glyphicon-arrow-right" style="margin-top: 15px; height:36px; opacity:1" value="" />
                }
            </th>
            @foreach (var item in Model)
            {

            <tr>
                <td class="col-md-2">
                    <span style="font-size: 17px;">@Html.DisplayFor(modelItem => item.movieName)</span>
                </td>
                <td class="col-md-2">
                    <span style="font-size: 17px;">@Html.DisplayFor(modelItem => item.movieReleaseDate)</span>
                </td>
                <td class="col-md-1">
                    <span style="font-size: 17px;">@Html.DisplayFor(modelItem => item.employeeName)</span>
                </td>
                <td class="col-md-1">
                    <span style="font-size: 17px;">@Html.DisplayFor(modelItem => item.Role)</span>
                </td>
                <td class="col-md-3 col-md-offset-3">
                </td>

            </tr>
                <tr>
                    <td colspan="12">
                        <p style="font-size: 17px; font-style: italic; font-family: 'Roboto', sans-serif">
                            Movie ID: @Html.DisplayFor(modelItem => item.movieID)
                            <br />
                            Description: @Html.DisplayFor(modelItem => item.movieDescription)
                            <br />
                            Starring: // ADD LIST OF EMPLOYEES HERE (item.employeeList)

                        </p>
                    </td>
                </tr>
            }
        </table>

enter image description here

如果需要任何其他信息,请告诉我们。

谢谢!

1 个答案:

答案 0 :(得分:1)

我认为你应该在视图模型中添加一个属性IEnumerable<string> employeeNames

var query =
from m in db.Movies
from me in m.MovieEmployees
where (parameter == 1 && m.Name.Contains(searchString)) || 
      (parameter == 2 && me.Employee.Name.Contains(searchString)) || 
      (parameter == 3 && me.Role.RoleType.Contains(searchString))
select new StarringViewModel
{ 
    employeeID = e.ID,
    movieID = m.ID, roleID = r.ID,
    movieName = m.Name, 
    movieDescription = m.Description, 
    movieReleaseDate = m.ReleaseDate, 
    employeeBirthdate = me.Employee.Birthday, 
    employeeName = me.Employee.Name, Role = me.Role.RoleType,
    employeeNames = m.MovieEmployees
                    .Selext(x => x.Employee.Name)
};

(顺便说一句,请注意,我使用导航属性而不是连接)。

您可以通过连接员工来显示员工:

string.Join(", ", employeeNames)

在视图中,或在视图模型中的EmployeeNamesString属性中。

如何在视图模型中执行此操作的示例:

public string EmployeeNamesString
{
    get { return string.Join(", ", this.employeeNames); }
}

并在视图中显示EmployeeNamesString