代码之家  ›  专栏  ›  技术社区  ›  Syntax Error ine

Linq查询不会从另一个对象中的对象返回结果

  •  2
  • Syntax Error ine  · 技术社区  · 6 年前

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    namespace ConsoleApp2
    {
        class Program
        {
            public class Associate
            {
                public string AssocType { get; set; }
                public Contact contact { get; set; }
                public Associate(string assocType)
                {
                    AssocType = assocType;
                    //contact = new Contact("Contact 1", 1234);
                    //contact2 = new Contact("Contact 2", 23456);
                }
            }
            public class Contact
            {
                string v1;
                int v2;
                public Contact(string v1, int v2) { this.v1 = v1; this.v2 = v2; }
                public string Name { get; set; }
                public int ID { get; set; }
            }
    
            static void Main(string[] args)
            {
                Associate assoc1 = new Associate("Type1");
                assoc1.contact = new Contact("Contact 1", 9876);
                Associate assoc2 = new Associate("Type2");
                assoc2.contact = new Contact("Contact 2", 1234);
                List<Associate> aList = new List<Associate>
                {
                    assoc1,assoc2
                };
                var contactname = aList
                    .Where(s => s.AssocType == "Type1")
                    .Select(s => s.contact.Name)
                    .FirstOrDefault();
                Console.Write(contactname);
                Console.Read();
    
            }
        }
    }
    

    这个 contactname 变量显示为null,但应返回 Contact 1 一串。我已签入调试器,所有对象都已正确设置并具有适当的值。我做错了什么?

    var contact name = aList... 但我不认为它是正确的,因为它不会编译,因为 Type inference failed .

            var name = from Associate in aList
                       from Contact in Associate
                       where Associate.AssocType == "Type1"
                       select Contact.Name;
    
    2 回复  |  直到 6 年前
        1
  •  3
  •   Ousmane D.    6 年前

    在你的 Contact Name 属性,以便在执行以下管道时:

    var contactname = aList.Where(s => s.AssocType == "Type1")
                           .Select(s => s.contact.Name)
                           .FirstOrDefault();
    

    它回来了 null 无效的 FirstOrDefault 无效的 价值 属性,因为它从未被设置)。

    var count = aList.Count(s => s.AssocType == "Type1" && s.contact.Name == null);
    Console.Write(count); // prints  --> 1
    

    你的意思是:

    var contactname = aList.Where(s => s.AssocType == "Type1")
                           .Select(s => s.contact.v1) // note the change
                           .FirstOrDefault();
    

    假设 v1

    名称 接触 对象在上述管道之前的某个时间。


    最后,为了完整性,如果需要查询语法,请执行以下操作:

    var result = (from a in aList
                  where a.AssocType == "Type1"
                  select a.contact.Name)
                 .FirstOrDefault();
    
        2
  •  0
  •   Nikhil Vartak    6 年前

    联系人姓名是 null

    公共联系人(字符串v1,int v2){this.v1=v1;this.v2=v2;}

    这两者中的任何一个都应该是

    public Contact(string v1, int v2) { Name = v1; ID = v2; }
    

    public string Name { get { return v1; } }