【问题标题】:How to Use LINQ to query to extract Subqueries如何使用 LINQ 查询提取子查询
【发布时间】:2021-12-28 16:51:07
【问题描述】:

我正在尝试在下面的主类中编写代码。我需要的是: 字符串格式。

     class Program
    {
        static void Main(string[] args)
        {
            //Console.WriteLine("Hello World");
            List<Person> human = new List<Person>()
            {
              new Person("Sarah", 18), new Person("Peter", 33),
              new Person("Paul", 29), new Child("Kevin", 15, true), new Child("Maria", 9, false)
            };
 
          
        }
//
        class Person
        {
            public string Firstname { get; set; }
            public int Age { get; set; }

            public Person(string fn, int ag)
            {
                this.Firstname = fn;
                this.Age = ag;
            }

        }
//
        class Child : Person   
        {
            public bool IsMale { get; set; }

            public Child(string fn, int ag, bool isM) : base(fn, ag) 
            {
                this.IsMale = isM;
            }

        }
////new feature:
public virtual string CalMe()
{
return "Hiii";
}
    }

能否请您在评论中更正我的答案或在那里写一个新答案? 谢谢

【问题讨论】:

  • 请避免过度编辑问题,以免使现有答案无效。
  • class, List&lt;Car&gt;, new 不是有效的 C。对于标记为 C 的问题,最好发布可编译的 C 代码。
  • 你不断改变你的问题。对于试图回答它的人来说,这非常令人沮丧。此外,以目前的形式,它完全不清楚你想要什么,你尝试了什么,以及你将如何需要这种“字符串格式”。

标签: c# linq


【解决方案1】:

我不同意 Dmitry 的回答,但我认为将检查组合成一个可能会使代码更流畅地阅读

human.Where(h => h is Child c && !c.IsMale)

至于为什么你的尝试没有成功;并非列表中的每个元素都是 Child(Child 是 Person,但不一定相反)-它们都能够表现得像 Person,但要成为 Child,则需要进行强制转换才能访问仅可用的属性在孩子身上


您已经大量编辑了您的问题,但没有说明新标准是什么。模式保持不变:

cars.Where(c => c is SubCar sc && sc.SomeSubCarOnlyProperty == someValue

【讨论】:

  • 感谢您的解释 :) 您能否也写下如何通过 LINQ 提取以查询具有“所有属性”的 18 岁以下人员的列表?我写的是这个,但我不知道如何包含所有属性: IEnumerable FilterOurQuery = human.Where(p => p.Age
  • 还要记住,您的过滤列表(如果您已经完成 Where(p=&gt;p is Child ...) )充满了实际上是 Child 类型的对象,因此您可以将它们强制转换为 Child 并访问 IsMale:foreach(var c in xxx) Console.WriteLine("{0} {1} {2}", p.Firstname, p.Age, (p as Child).IsMale);。 . 你不能用你的 p.Age
  • 上述评论中的错字更正:虽然你的p.Age&lt;= 18 不能安全地做到这一点,因为你的&lt;=28 也会把莎拉带进来,她是一个人,而不是一个孩子
  • !(p is Child) &amp;&amp; p.Age &lt; whatever
  • 再次,您正在以一种接近破坏已发布答案的方式编辑您的问题。请提出一个新问题。 StackOverflow 就像“每个问题及其答案的 wiki”;它不是某种支持票务系统,您只需询问一件事,然后在编写整个应用程序时反复编辑您的问题/要求/流程
【解决方案2】:

你可以尝试OfType()构造来过滤掉Persons中的所有Children:

var femaleChildren = human
  .OfType<Child>()                // children only
  .Where(child => !child.IsMale); // which are not male

foreach (var child in femaleChildren)
  Console.WriteLine($"{child.Firstname} {child.Age}");

编辑:如果你不想使用OfType,你可以把SelectWhere

var femaleChildren = human
  .Select(item => item as Child)  // either Child or null
  .Where(item => item != null)    // children only, no nulls 
  .Where(child => !child.IsMale); // which are not male 

【讨论】:

  • @arash6657:很抱歉打错了,应该是Firstname。我已经编辑了答案
  • @arash6657:它以Firstname Age 格式打印每个child - Console.WriteLine($"{child.Firstname} {child.Age}");,例如Maria 9
  • @arash6657:如果您不想使用OfType&lt;T&gt;,您可以尝试asisWhere 中过滤掉Children。请看我的编辑`
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-03
  • 1970-01-01
  • 1970-01-01
  • 2020-11-30
相关资源
最近更新 更多