我有两个表LookUpCodes和LookUpValues它们被定义如下:简单的Linq查询联接两个表,并返回一个集合
public partial class LookUpCodes
{
public int Id { get; set; }
public int? CodeId { get; set; }
public string Code { get; set; }
public string Description { get; set; }
}
public partial class LookUpValues
{
public int Id { get; set; }
public int CodeId { get; set; }
public string CodeValue { get; set; }
public bool? IsActive { get; set; }
}
每个LookUpCode可以有多个与之相关的值。我想传入一个代码并获取关联的值列表。
这可能是一个常见的问题,因为我已经在任何地方看到了这个问题,如果有人可以解释如何构建正确的查询,我不会寻求答案本身。
这是我迄今所做的:
public IEnumerable<LookUpValues> GetValuesByCode(string cd)
{
var query = from code in _context.LookUpCodes
join values in _context.LookUpValues on code.CodeId equals values.CodeId
where code.Code == cd
select new { LookUpValues = values };
return (IEnumerable<LookUpValues>) query.ToList();
}
感谢您的及时回应和真正的好解释。我还有一个问题,我如何按照CodeValue属性的降序排列值? –
不客气。你可以做到这一点,就像你已经描述过的那样简单。就在select语句放置这个'orderby values descending' – Christos
谢谢你的教育。我希望这也可以帮助其他人。 –