Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 具有自定义列表的LINQ中的对象_C#_Linq - Fatal编程技术网

C# 具有自定义列表的LINQ中的对象

C# 具有自定义列表的LINQ中的对象,c#,linq,C#,Linq,我在阐述这个问题: 我不得不将联系人列表从一个列表更改为一个包含姓名和Id的列表 public class DataItem { public String Location { get; set; } public List<ContactInfo> ContactsList { get; set; } } public class ContactInfo { public String PersonName { get; set; } publi

我在阐述这个问题:

我不得不将联系人列表从一个列表更改为一个包含姓名和Id的列表

public class DataItem
{
    public String Location { get; set; }
    public List<ContactInfo> ContactsList { get; set; }
}

public class ContactInfo
{
    public String PersonName { get; set; }
    public Int32 PersonId { get; set; }
}

        var myData= from table in sqlResults.AsEnumerable()
                        group table by table["LOCATION"] into groupby
                        select new DataItem
                        {
                            Location = groupby.Key.ToString(),
                            ContactsList = groupby.Select(row => new ContactInfo 
                            {
                                PersonName =   row["PERSON"].ToString(), 
                                PersonId = (Int32)row["PERSONID"]
                            }).ToList()
                        }; 
//TreeView
tv.DataContext = BrokerData;

而不是弱类型索引器

PersonId = (Int32)row["PERSONID"]
使用强类型
字段
扩展方法

PersonId = row.Field<Int32>("PERSONID")

PersonId=row.Field(“PersonId”)

在您的类示例中,我们甚至没有看到
ContactTypeContactId
。我猜行[“PERSONID”]不是
Int32
,而是
长的
。引发异常的位置在哪里?在那个位置,被投射的对象的实际值是什么,它被投射到什么位置?很有可能,在这一点上,您需要将它转换为它真正的样子,然后将它转换为您需要的样子,而不是转换为您需要的样子also@Servy-当我试图查看我的数据时,它被抛出。我需要转换什么?
PersonId = row.Field<Int32>("PERSONID")
public Int32? PersonId { get; set; }
PersonId = row.Field<Int32?>("PERSONID")