【发布时间】:2021-08-10 01:29:52
【问题描述】:
我有两个表,我想从中获取数据并将其返回给 API 以供使用。两个表之间是有关系的。
[![在此处输入图片描述][1]][1]
当我尝试获取数据时,它只返回一行,这不是我想要的。
如何返回与ResellerId 相关的所有数据(8435 个示例)?
这是我的代码:
public RateSheetModel GetRateSheet(int resellerId)
{
// This only returns only one row.
// How can I get all rows for all the same Id?
var rateSheetDetails = (from r in _Context.WholesaleRateSheet
where r.ResellerId == resellerId
select r).First();
}
模型
public class WholesaleRateSheetMarkup
{
[Key]
public int RateSheetMarkupId { get; set; }
[Required]
public int ResellerId { get; set; }
[StringLength(50)]
public string RatesheetName { get; set; }
}
public class WholesaleRateSheet
{
[Key]
public int RateSheetId { get; set; }
[Required]
public int RateSheetMarkupId { get; set; }
[Required]
public int ResellerId { get; set; }
public string CountryCode { get; set; }
public string Description { get; set; }
public decimal Peak { get; set; }
public bool IsSouthAfricanRate { get; set; }
public bool IsInertnationRate { get; set; }
public bool IsSpecificRate { get; set; }
public int DestinationGroupSetId { get; set; }
public int DestinationGroupId { get; set; }
public string DestinationLookup { get; set; }
public DateTime CreatedDate { get; set; }
public string CreatedByUsername { get; set; }
public DateTime LastUpdatedDate { get; set; }
public string UpdatedByUsername { get; set; }
}
【问题讨论】:
-
为什么在指定
First()时期望不止一行? -
哦不!让我现在改一下
-
我现在已将其更改为 (.ToList()) 并且我从一张表中获取了我想要的所有数据。如何连接两个表并仍然使用 where 子句?
-
这能回答你的问题吗? Join/Where with LINQ and Lambda
-
这就是 Include 的用途
标签: c# entity-framework join