Queryable.Join 使用内部表的 WHERE 子句?

huangapple go评论67阅读模式
英文:

Queryable.Join with the WHERE clause using inner table?

问题

我试图使用Queryable.Join方法将对象列表连接在一起,但我想针对内部表使用WHERE子句。在这种情况下,我无法颠倒连接表的顺序。当我添加WHERE子句时,它只“看到”外部表中的字段。以下是重现我的问题的示例代码:

using System;
using System.Collections.Generic;
using System.Linq;

namespace JoinsWithIQueriable
{
    internal class Program
    {
        class Person
        {
            public string Name { get; set; }
            public int Id { get; set; }
        }

        class Pet
        {
            public string Name { get; set; }
            public int WeightInLbs { get; set; }
            public Person Owner { get; set; }

            public Species PetSpecies { get; set; }

        }

        class Species
        {
            public string Name { get; set; }
        }

        static void Main(string[] args)
        {
            Species dog = new Species { Name = "Dog" };
            Species cat = new Species { Name = "Cat" };

            Person magnus = new Person { Name = "Hedlund, Magnus", Id = 1 };
            Person terry = new Person { Name = "Adams, Terry", Id = 2 };
            Person charlotte = new Person { Name = "Weiss, Charlotte", Id = 3 };
            Person alergictopets = new Person { Name = "Scratchy, Itchy", Id = 4 };

            Pet barley = new Pet { Name = "Barley", WeightInLbs = 10, Owner = terry, PetSpecies = cat };
            Pet boots = new Pet { Name = "Boots", WeightInLbs = 11, Owner = terry, PetSpecies = cat };
            Pet whiskers = new Pet { Name = "Whiskers", WeightInLbs = 12, Owner = charlotte, PetSpecies = cat };
            Pet daisy = new Pet { Name = "Daisy", WeightInLbs = 40, Owner = magnus, PetSpecies = dog };

            List<Person> people = new List<Person> { magnus, terry, charlotte, alergictopets };
            List<Pet> pets = new List<Pet> { barley, boots, whiskers, daisy };
            List<Species> species = new List<Species> { dog, cat };

            // 使用Queryable.Join方法将Person对象列表和Pet对象列表连接,
            // 创建一个包含宠物名称和拥有宠物的人员名称的匿名类型的列表。
            Console.WriteLine("query");

            var query = people.AsQueryable().Join(pets,
                person => person,
                pet => pet.Owner,
                (person, pet) =>
                    new { OwnerName = person.Name, Pet = pet.Name, Weight = pet.WeightInLbs });

            foreach (var obj in query)
            {
                Console.WriteLine(
                    "Owner {0} - Pet {1} - Pet weight {2} ",
                    obj.OwnerName,
                    obj.Pet,
                    obj.Weight.ToString());
            }

            /*
                此代码生成以下输出:

                Owner Hedlund, Magnus - Pet Daisy - Pet weight 40
                Owner Adams, Terry - Pet Barley - Pet weight 10
                Owner Adams, Terry - Pet Boots - Pet weight 11
                Owner Weiss, Charlotte - Pet Whiskers - Pet weight 12
            */

            Console.WriteLine("querywithwhere");

            // 想要筛选体重小于12磅的宠物
            var querywithwhere = people.AsQueryable()
                .Join(pets,
                person => person,
                pet => pet.Owner,
                (person, pet) =>
                    new { OwnerName = person.Name, Pet = pet.Name, Weight = pet.WeightInLbs });

            foreach (var obj in querywithwhere)
            {
                Console.WriteLine(
                    "Owner {0} - Pet {1} - Pet weight {2} ",
                    obj.OwnerName,
                    obj.Pet,
                    obj.Weight.ToString());
            }

            Console.WriteLine("Press any key to continue...");
            Console.ReadLine();

        }
    }
}

希望这对你有所帮助。

英文:

I'm trying to use the Queryable.Join method to join Lists of objects together, but I want to use the WHERE clause against the inner table. I cannot reverse the order of the join tables in this case. When I add a WHERE clause it only "sees" the fields in the outer table. Here's a sample that reproduces my problem:

using System;
using System.Collections.Generic;
using System.Linq;
namespace JoinsWithIQueriable
{
internal class Program
{
class Person
{
public string Name { get; set; }
public int Id { get; set; }
}
class Pet
{
public string Name { get; set; }
public int WeightInLbs { get; set; }
public Person Owner { get; set; }
public Species PetSpecies { get; set; }
}
class Species
{
public string Name { get; set; }
}
static void Main(string[] args)
{
Species dog = new Species { Name = &quot;Dog&quot; };
Species cat = new Species { Name = &quot;Cat&quot; };
Person magnus = new Person { Name = &quot;Hedlund, Magnus&quot;, Id = 1};
Person terry = new Person { Name = &quot;Adams, Terry&quot;, Id = 2 };
Person charlotte = new Person { Name = &quot;Weiss, Charlotte&quot;, Id = 3 };
Person alergictopets = new Person { Name = &quot;Scratchy, Itchy&quot;, Id = 4 };
Pet barley = new Pet { Name = &quot;Barley&quot;, WeightInLbs = 10, Owner = terry, PetSpecies = cat };
Pet boots = new Pet { Name = &quot;Boots&quot;, WeightInLbs = 11, Owner = terry, PetSpecies = cat };
Pet whiskers = new Pet { Name = &quot;Whiskers&quot;, WeightInLbs = 12, Owner = charlotte, PetSpecies = cat };
Pet daisy = new Pet { Name = &quot;Daisy&quot;, WeightInLbs = 40, Owner = magnus, PetSpecies = dog};
List&lt;Person&gt; people = new List&lt;Person&gt; { magnus, terry, charlotte, alergictopets };
List&lt;Pet&gt; pets = new List&lt;Pet&gt; { barley, boots, whiskers, daisy };
List&lt;Species&gt; species = new List&lt;Species&gt; { dog, cat};
// Join the list of Person objects and the list of Pet objects
// to create a list of person-pet pairs where each element is
// an anonymous type that contains the name of pet and the name
// of the person that owns the pet.
Console.WriteLine(&quot;query&quot;);
var query = people.AsQueryable().Join(pets,
person =&gt; person,
pet =&gt; pet.Owner,
(person, pet) =&gt;
new { OwnerName = person.Name, Pet = pet.Name, Weight = pet.WeightInLbs });
foreach (var obj in query)
{
Console.WriteLine(
&quot;Owner {0} - Pet {1} - Pet weight {2} &quot;,
obj.OwnerName,
obj.Pet, 
obj.Weight.ToString());
}
/*
This code produces the following output:
Owner Hedlund, Magnus - Pet Daisy - Pet weight 40
Owner Adams, Terry - Pet Barley - Pet weight 10
Owner Adams, Terry - Pet Boots - Pet weight 11
Owner Weiss, Charlotte - Pet Whiskers - Pet weight 12
*/
Console.WriteLine(&quot;querywithwhere&quot;);
//Want to filter for pets that weight less than 12 lbs
var querywithwhere = people.AsQueryable()
.Join(pets,
person =&gt; person,
pet =&gt; pet.Owner,
(person, pet) =&gt;
new { OwnerName = person.Name, Pet = pet.Name, Weight = pet.WeightInLbs });
foreach (var obj in querywithwhere)
{
Console.WriteLine(
&quot;Owner {0} - Pet {1} - Pet weight {2} &quot;,
obj.OwnerName,
obj.Pet,
obj.Weight.ToString());
}
Console.WriteLine(&quot;Press any key to continue...&quot;);
Console.ReadLine();
}
}
}

答案1

得分: 1

要么你可以在与 people 连接之前使用包含记录 WeightInLbs &lt; 12 的方式筛选 pets

var querywithwhere = people.AsQueryable()
    .Join(pets.Where(x =&gt; x.WeightInLbs &lt; 12),
        person =&gt; person,
        pet =&gt; pet.Owner,
        (person, pet) =&gt;
            new { OwnerName = person.Name, Pet = pet.Name, Weight = pet.WeightInLbs });

或者你可以在连接后使用 .Where() 子句筛选记录。

var querywithwhere = people.AsQueryable()
    .Join(pets,
        person =&gt; person,
        pet =&gt; pet.Owner,
        (person, pet) =&gt;
            new { OwnerName = person.Name, Pet = pet.Name, Weight = pet.WeightInLbs })
    .Where(x =&gt; x.Weight &lt; 12);
英文:

Either you can filter the pets with containing the record with WeightInLbs &lt; 12 before join with people

var querywithwhere = people.AsQueryable()
	.Join(pets.Where(x =&gt; x.WeightInLbs &lt; 12),
		person =&gt; person,
		pet =&gt; pet.Owner,
		(person, pet) =&gt;
			new { OwnerName = person.Name, Pet = pet.Name, Weight = pet.WeightInLbs });

Or you can filter the record after joining with .Where() clause.

var querywithwhere = people.AsQueryable()
	.Join(pets,
		person =&gt; person,
		pet =&gt; pet.Owner,
		(person, pet) =&gt;
			new { OwnerName = person.Name, Pet = pet.Name, Weight = pet.WeightInLbs })
	.Where(x =&gt; x.Weight &lt; 12);

huangapple
  • 本文由 发表于 2023年6月5日 19:54:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76406170.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定