【问题标题】:Retrieving related data using linq使用 linq 检索相关数据
【发布时间】:2012-10-17 15:00:37
【问题描述】:

我有一个首先使用 EF 代码的 MVC 应用程序。我将用户添加到系统并输入养老金详细信息,其中一部分是链接到名为 PensionBenefitLevel 的模型的下拉列表。这是模型 -

[Key]
public int PensionBenefitLevelID { get; set; }
public string DisplayText { get; set; }
public int EmployeePercentage { get; set; }
public int EmployerPercentage { get; set; }

public virtual ICollection<Pension> Pension { get; set; }

注册时,我有来自下拉列表的 PensionBenefitLevelID,但在我的控制器中,我将使用与该 ID 相关的 EmployerPercentage 值执行计算。谁能指出我正确的方向?

我是否需要在控制器中创建一个变量并使用 linq 查询来取回该值?我找不到任何类似的例子,所以如果你能指出一个也很棒的例子。

【问题讨论】:

  • 你需要进行什么计算?

标签: asp.net-mvc linq ef-code-first


【解决方案1】:

如果我对问题的理解正确,您想取回与PensionBenefitLevelID 对应的实体,并对EmployerPercentage 字段进行计算。

由于您没有提到您在 EF 中使用的模式(存储库、工作单元等),我只能给您一个一般性的答案:

var entity = [Your DB Context].[Your Entity].GetById(pensionBenefitLevelID);

if(entity != null)
{
    [Calculation]
} 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-16
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-06
    相关资源
    最近更新 更多