0

给定两个表,Project 和 Expense 以及字段

Project.ProjectKey
Project.Budget
Expense.ProjectKey
Expense.Amount

我将如何编写一个 Linq 语句来给出包括Project.Budget减去所有在内的结果我将Expense.Amounts? 如何在 MVC 4 视图中引用该引用?

这是我所拥有的......但它并不接近:

var projects = from s in db.Project
               join d in db.Expense
               on s.ProjectKEY equals d.ProjectKEY
               select s;

帮助表示赞赏!

风景:

@model IEnumerable<TETS_DAL.Project>

@{
    ViewBag.Title = "Projects";
}

<h2>All Projects</h2>

<p>
    @Html.ActionLink("Create New Project", "Create")
</p>
@using (Html.BeginForm())
{
    <table style="padding: 5px 5px 5px 5px;">
        <tr>
            <th>
                <!--@Html.DisplayNameFor(model => model.ProjectKEY)-->
            </th>
            <th>Project<br />
                Number
            </th>
            <th>Office
            </th>
            <th>Project<br />
                Description
            </th>
            <th>Start<br />
                Date
            </th>
            <th>End<br />
                Date
            </th>
            <th>Current<br />
                Period<br />
                Start
            </th>
            <th>Current<br />
                Period<br />
                End
            </th>
            <th>Total<br />
                Budget
            </th>
            <th>Total<br />
                Budget<br />
                Balance
            </th>
            <th>Current<br />
                Budget
            </th>
            <th>Current<br />
                Budget<br />
                Balance
            </th>
            <th></th>
        </tr>

        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.HiddenFor(modelItem => item.ProjectKEY)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.ProjectNumber)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Division.DivisionName)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.ProjectDescription)
                </td>
                <td>
                    @String.Format("{0:d}", item.StartDate)
                </td>
                <td>
                    @String.Format("{0:d}", item.OverallEndDate)
                </td>
                <td>
                    @String.Format("{0:d}", item.CurrentPeriodStartDate)
                </td>
                <td>
                    @String.Format("{0:d}", item.CurrentPeriodEndDate)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.OverallTechnologyBudget)
                </td>
                <td>
                    @*  @Html.DisplayFor(modelItem => item.ExpenseItem.ExpenseAmount)*@
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.CurrentPeriodBudget)
                </td>
                <td></td>
                <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.ProjectKEY }) |
                @Html.ActionLink("Details", "Details", new { id = item.ProjectKEY }) |
                @Html.ActionLink("Expenses", "Index", "ExpenseItem", new { projectSearchString = item.ProjectNumber }, null)
                </td>
            </tr>
        }

    </table>
}
4

2 回答 2

0

试试这个(对不起,它在方法语法中):

var projects = db.Project.Join(db.Expense, s => s.ProjectKEY, d => d.ProjectKEY,
                   (s, d) => new { Budget = s.Budget, Expenses = d.Amount })
               .Select(a => a.Budget - a.Expenses);
于 2013-07-02T19:59:35.447 回答
0

检查这个:

    // Mock data
    var Project = new List<dynamic>
                      {
                          new { ProjectKey = 1, Budget = 500 },
                          new { ProjectKey = 2, Budget = 1000 },
                      };

    var Expense = new List<dynamic>
                      {
                          new { ProjectKey = 1, Amount = 10 },
                          new { ProjectKey = 1, Amount = 20 },
                          new { ProjectKey = 1, Amount = 30 },
                          new { ProjectKey = 2, Amount = 15 },
                          new { ProjectKey = 2, Amount = 25 },
                      };

    var data = (from p in Project
               join ex in Expense on p.ProjectKey equals ex.ProjectKey
               group ex by p into exgroup
               select new
               {
                   ProjectKey = exgroup.Key.ProjectKey,
                   Budget = exgroup.Key.Budget,
                   BudgetWithoutSumOfAmount = exgroup.Key.Budget - exgroup.Sum(g => g.Amount)
               }).ToList();

   // here you get in data variable list of objects:
   //
   //       [0] { ProjectKey = 1,
   //             Budget = 500,
   //             BudgetWithoutSumOfAmount = 440, }
   //       [1] { ProjectKey = 2,
   //             Budget = 1000,
   //             BudgetWithoutSumOfAmount = 960, }

您还可以从 select 语句中获取任何其他Project属性exgroup.Key.PropertyName

于 2013-07-02T19:48:40.447 回答