简体   繁体   English

C#LINQ使用where子句连接2个表

[英]C# LINQ join 2 tables with where clause

I am trying to join 2 tables while filtering with where clause. 我正在尝试使用where子句进行过滤时join 2个表。 The data looks like this: 数据如下所示:

Category
Name Selected
A    0
B    1
C    0

SubCategory
Name ParentCategory Selected
s1   A              0
s2   B              1
s3   B              0

Expected results: 预期成绩:

ParentCatName SubCatName SubCatSelected
B             s2         1
B             s3         0

Actual results: 实际结果:

ParentCatName SubCatName SubCatSelected
B             s2         1
B             s3         1  <-- should be 0

The code I am using is this: 我使用的代码是这样的:

IEnumerable<Category> result =
    from s in subcategories
    join c in categories
    on s.Parent equals c.Name into t
    from r in t.DefaultIfEmpty()
    where r == null ? false : r.Selected == true
    select new Category
    {
        Name = s.Name,
        Parent = (r == null ? string.Empty : r.Name),
        Selected = r.Selected
    };

EDIT: Something that helped me get clarity was to temporarily rewrite this to see the resulting data structures... 编辑:帮助我明确的东西是暂时重写这个以查看结果数据结构......

var result =
    from s in subcategories
    join c in categories
    on s.Parent equals c.Name into t
    from r in t.DefaultIfEmpty()
    select new
    {
        s, r
    };

Then I came up with the answer to the filtering of selected categories. 然后我想出了过滤所选类别的答案。 See my answer below.. 请参阅下面的答案..

Don't overcomplicate the things. 不要过分复杂化。 What you are trying to achieve is to filter subcategories by the selected categories. 您要实现的目标是按所选类别过滤子类别。 You can get the desired result with the following simple query 您可以使用以下简单查询获得所需的结果

var result = from s in subcategories
             join c in categories on s.Parent equals c.Name
             where c.Selected
             select s;

It looks like you're setting it wrong. 看起来你错了。 If r == null then you're setting it to false , otherwise you're setting it to true here: r.Select == true . 如果r == null那么你将它设置为false ,否则你在这里将它设置为truer.Select == true

Just by reading your query it looks like you may not need that where clause at all. 只需阅读您的查询,您可能根本不需要where子句。

You probably want something like this: 可能想要这样的东西:

IEnumerable<Category> result =
    from s in subcategories
    join c in categories
    on s.Parent equals c.Name into t
    from r in t.DefaultIfEmpty()
    select new Category
    {
        Name = s.Name,
        Parent = (r == null ? string.Empty : r.Name),
        Selected = r.Selected
    };

Or if you need to do the null check then do this: 或者,如果您需要执行null检查,请执行以下操作:

IEnumerable<Category> result =
    from s in subcategories
    join c in categories
    on s.Parent equals c.Name into t
    from r in t.DefaultIfEmpty()
    where r != null //I added the null check here
    select new Category
    {
        Name = s.Name,
        Parent = (r.Name), //I removed the null check here
        Selected = r.Selected
    };

I think, your t value inculudes categoies list. 我认为,你的t值包含了categoies列表。 t must be include subcategory list and then you can take selected value of subcategory. t必须包含子类别列表,然后您可以选择子类别的选定值。 So you always get selected value as 1 pls try this: 所以你总是得到选择的价值为1请尝试这样:

IEnumerable<SubCategory> result =
    from c in categories
    join s in subcategories
    on c.Name equals s.Parent into t
    from r in t.DefaultIfEmpty()
    where r == null ? false : r.Selected == true
    select new SubCategory
    {
        Name = s.Name,
        Parent = (r == null ? string.Empty : r.Name),
        Selected = r.Selected
    };

OBS:I'm not try this now. OBS:我现在不试试。 But I think works. 但我觉得有效。

Okay. 好的。 So I did some more looking and came up with this... 所以我做了一些寻找并想出了这个......

IEnumerable<Category> result =
    from s in subcategories
    join c in categories.Where(f => f.Selected)
    on s.Parent equals c.Name into t
    from r in t.DefaultIfEmpty()
    where r == null ? false : true
    select new Category
    {
        Name = s.Name,
        Parent = s.Name,
        Selected = s.Selected,
    };

To filter join to only selected parent category, I have added lambda expression right to that data. 要将连接过滤到仅选定的父类别,我已将lambda表达式添加到该数据中。

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

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