简体   繁体   English

连接两个表并返回一个集合的简单Linq查询

[英]Simple Linq query that joins two tables and returns a collection

I have two tables LookUpCodes and LookUpValues they are defined as below: 我有两个表LookUpCodes和LookUpValues,它们的定义如下:

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; }
}

Each LookUpCode can have multiple Values associated with it. 每个LookUpCode可以具有多个与其关联的值。 I want to pass in a code and get associated list of values back. 我想传递代码并返回相关的值列表。

This is probably a common question as I have seen this everywhere, I am not looking for an answer per se, if someone can just explain how to build the proper query I would be obliged. 这可能是一个常见的问题,因为我到处都看到了这个问题,如果有人可以解释如何构建正确的查询,我本身就不会在寻找答案。

Here is what I have done so far: 到目前为止,这是我所做的:

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();
}

You are very close to that you are looking for: 您非常接近您要寻找的:

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 values;

    return query;
}

Since you have written the join , I assume that you have understood how it works. 既然您已经编写了join ,那么我假设您已经了解了它是如何工作的。 However let's revisit it: 但是,让我们重新回顾一下:

from a in listA
join b in listB
on a.commonId equals b.commonId

In the above snippet we join the contents of listA with the contents of listB and we base their join on a commonId property existing in items of both lists. 在上面的代码段中,我们将listA的内容与listA的内容连接listB并且我们的join基于两个列表项中都存在的commonId属性。 Apparently the pair of a and b that fulfill the join criterion it would form one of the possible many results. 显然,满足join条件的ab对将形成可能的许多结果之一。

Then the where clause is applied on the results of the join. The joined items that pass the 然后将where子句应用于联接的结果join. The joined items that pass the join. The joined items that pass the where filter is the new result. Even at this point the results is still pairs of join. The joined items that pass the where filter is the new result. Even at this point the results is still pairs of join. The joined items that pass the filter is the new result. Even at this point the results is still pairs of filter is the new result. Even at this point the results is still pairs of a and b`. filter is the new result. Even at this point the results is still pairs of a and b`对。

Last you project, using the select keyword each pair of the results to a new object. 最后一个项目,使用select关键字将每对结果复制到一个新对象。 In your case, for each pair of code and values that passed also the where filter, you return only the values . 对于您的情况,对于还通过where过滤器的每对codevalues ,您仅返回values

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM