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
使用linq lambda从具有多个链接表的数据集中填充C#类_C#_Linq_Lambda - Fatal编程技术网

使用linq lambda从具有多个链接表的数据集中填充C#类

使用linq lambda从具有多个链接表的数据集中填充C#类,c#,linq,lambda,C#,Linq,Lambda,我正试图从SQL返回的数据集中填充C#类(contractor)的列表(lstContractor),其中包含字段和列表。数据集返回多个由id链接的表 我已经设法让它所有的工作,除了1个字段,这是唯一的字段,我需要从表中它是。当我试图使用LINQ选择它时,我得到的是System.Data.EnumerablerRowCollection1[System.String]`而不是字段的内容 我的代码如下所示: lstContractor = dsContractor.Tables[0].As

我正试图从SQL返回的
数据集
中填充C#类(
contractor
)的列表(
lstContractor
),其中包含字段和列表。
数据集
返回多个由id链接的表

我已经设法让它所有的工作,除了1个字段,这是唯一的字段,我需要从表中它是。当我试图使用LINQ选择它时,我得到的是
System.Data.EnumerablerRowCollection
1[System.String]`而不是字段的内容

我的代码如下所示:

    lstContractor = dsContractor.Tables[0].AsEnumerable().Select(contractor => new Contractor
    {
        intContractorId = contractor.Field<int>("ID"),
        strFirstName = contractor.Field<string>("FirstName"),
        strLastName = contractor.Field<string>("LastName"),
        strProfile = dsContractor.Tables[7].AsEnumerable().Where(profile => profile.Field<int>("ContractorId") == contractor.Field<int>("Id")).
            Select(profile => profile.Field<string>("ContractorProfile")).ToString(),
        // populate addresses
        Address = dsContractor.Tables[6].AsEnumerable().Where(address => address.Field<int>("ContractorId") == contractor.Field<int>("Id")).
                Select(address => new Address
                {
                    intId = address.Field<int>("ContractorId"),
                    strAddressLine1 = address.Field<string>("AddressLine1"),
                    strAddressLine2 = address.Field<string>("AddressLine2"),
                    strAddressLine3 = address.Field<string>("AddressLine3"),
                    strAddressLine4 = address.Field<string>("AddressLine4"),
                    strAddressLine5 = address.Field<string>("AddressLine5"),
                    strPostCode = address.Field<string>("PostCode")
                }).ToList<Address>(),

        // populate industries
        Industry = dsContractor.Tables[1].AsEnumerable().Where(Industry => Industry.Field<int>("ContractorId") == contractor.Field<int>("Id")).
            Select(target => new Industry
            {
                intId = target.Field<int>("ContractorId"),
                strIndustryName = target.Field<string>("IndustryName")
            }).ToList<Industry>(),

        // populate key skills
        KeySkill = dsContractor.Tables[2].AsEnumerable().Where(KeySkill => KeySkill.Field<int>("ContractorId") == contractor.Field<int>("Id")).
            Select(target => new KeySkill
            {
                intId = target.Field<int>("ContractorId"),
                strKeySkillName = target.Field<string>("KeySkillName")
            }).ToList<KeySkill>(),

        // populate email addresses
        EmailAddress = dsContractor.Tables[3].AsEnumerable().Where(EmailAddress => EmailAddress.Field<int>("ContractorId") == contractor.Field<int>("Id")).
            Select(target => new EmailAddress
            {
                intId = target.Field<int>("ContractorId"),
                strEmailAddress = target.Field<string>("EmailAddress"),
                strEmailType = target.Field<string>("EmailType")
            }).ToList<EmailAddress>(),

        // populate phone numbers
        PhoneNumber = dsContractor.Tables[4].AsEnumerable().Where(PhoneNumber => PhoneNumber.Field<int>("ContractorId") == contractor.Field<int>("Id")).
            Select(target => new PhoneNumber
            {
                intId = target.Field<int>("ContractorId"),
                strPhoneNumberType = target.Field<string>("PhoneType"),
                strPhoneNumber = target.Field<string>("PhoneNumber")
            }).ToList<PhoneNumber>(),

        Geography = dsContractor.Tables[5].AsEnumerable().Where(PhoneNumber => PhoneNumber.Field<int>("ContractorId") == contractor.Field<int>("Id")).
                Select(target => new Geography
                {
                    intId = target.Field<int>("ContractorId"),
                    strGeography = target.Field<string>("GeographyName")
                }).ToList<Geography>(),


    }).ToList<Contractor>();
    public int intContractorId;
public string strFirstName;
public string strMiddleName;
public string strLastName;
public string strDescription; 
public DateTime dtDOB;
public string strGender;
public string strProfile;
public int intIsFullTime;
public int intWorkMonday;
public int intWorkTuesday;
public int intWorkWednesday;
public int intWorkThursday;
public int intWorkFriday;
public int intWorkSaturday;
public int intWorkSunday;
public DateTime dtAvailableFrom;
public int intActive;
public decimal dcSubscrptionCost;
public DateTime dtRenewalDate;
public List<Address> Address;
public List<EmailAddress> EmailAddress;
public List<PhoneNumber> PhoneNumber;
public List<KeySkill> KeySkill;
public List<Geography> Geography;
public List<Industry> Industry;
public List<SubIndustry> SubIndustry;
lstContractor=dsContractor.Tables[0].AsEnumerable().Select(contractor=>newcontractor
{
intContractorId=承包商字段(“ID”),
strFirstName=承包商字段(“FirstName”),
strLastName=承包商字段(“LastName”),
strProfile=dsContractor.Tables[7].AsEnumerable()。其中(profile=>profile.Field(“ContractorId”)==contractor.Field(“Id”)。
选择(profile=>profile.Field(“ContractorProfile”)).ToString(),
//填充地址
Address=dsContractor.Tables[6].AsEnumerable()。其中(Address=>Address.Field(“ContractorId”)==contractor.Field(“Id”))。
选择(地址=>新地址
{
intId=地址字段(“合同ID”),
strAddressLine1=地址字段(“AddressLine1”),
strAddressLine2=地址字段(“AddressLine2”),
strAddressLine3=地址字段(“AddressLine3”),
strAddressLine4=地址字段(“AddressLine4”),
strAddressLine5=地址字段(“AddressLine5”),
strPostCode=地址字段(“邮政编码”)
}).ToList(),
//填充工业
Industry=dsContractor.Tables[1].AsEnumerable()。其中(Industry=>Industry.Field(“ContractorId”)==contractor.Field(“Id”))。
选择(目标=>新行业
{
intId=目标字段(“合同ID”),
strIndustryName=target.Field(“IndustryName”)
}).ToList(),
//填充关键技能
KeySkill=dsContractor.Tables[2].AsEnumerable().Where(KeySkill=>KeySkill.Field(“ContractorId”)==contractor.Field(“Id”))。
选择(目标=>newkeyskill
{
intId=目标字段(“合同ID”),
strKeySkillName=target.Field(“KeySkillName”)
}).ToList(),
//填充电子邮件地址
EmailAddress=dsContractor.Tables[3].AsEnumerable().Where(EmailAddress=>EmailAddress.Field(“ContractorId”)==contractor.Field(“Id”))。
选择(目标=>新电子邮件地址
{
intId=目标字段(“合同ID”),
strEmailAddress=target.Field(“电子邮件地址”),
strEmailType=target.Field(“EmailType”)
}).ToList(),
//填充电话号码
PhoneNumber=dsContractor.Tables[4].AsEnumerable().Where(PhoneNumber=>PhoneNumber.Field(“ContractorId”)==contractor.Field(“Id”))。
选择(目标=>新电话号码
{
intId=目标字段(“合同ID”),
strPhoneNumberType=target.Field(“PhoneType”),
strPhoneNumber=target.Field(“电话号码”)
}).ToList(),
Geography=dsContractor.Tables[5].AsEnumerable().Where(PhoneNumber=>PhoneNumber.Field(“ContractorId”)==contractor.Field(“Id”))。
选择(目标=>新地理位置)
{
intId=目标字段(“合同ID”),
strography=target.Field(“GeographyName”)
}).ToList(),
}).ToList();
该类如下所示:

    lstContractor = dsContractor.Tables[0].AsEnumerable().Select(contractor => new Contractor
    {
        intContractorId = contractor.Field<int>("ID"),
        strFirstName = contractor.Field<string>("FirstName"),
        strLastName = contractor.Field<string>("LastName"),
        strProfile = dsContractor.Tables[7].AsEnumerable().Where(profile => profile.Field<int>("ContractorId") == contractor.Field<int>("Id")).
            Select(profile => profile.Field<string>("ContractorProfile")).ToString(),
        // populate addresses
        Address = dsContractor.Tables[6].AsEnumerable().Where(address => address.Field<int>("ContractorId") == contractor.Field<int>("Id")).
                Select(address => new Address
                {
                    intId = address.Field<int>("ContractorId"),
                    strAddressLine1 = address.Field<string>("AddressLine1"),
                    strAddressLine2 = address.Field<string>("AddressLine2"),
                    strAddressLine3 = address.Field<string>("AddressLine3"),
                    strAddressLine4 = address.Field<string>("AddressLine4"),
                    strAddressLine5 = address.Field<string>("AddressLine5"),
                    strPostCode = address.Field<string>("PostCode")
                }).ToList<Address>(),

        // populate industries
        Industry = dsContractor.Tables[1].AsEnumerable().Where(Industry => Industry.Field<int>("ContractorId") == contractor.Field<int>("Id")).
            Select(target => new Industry
            {
                intId = target.Field<int>("ContractorId"),
                strIndustryName = target.Field<string>("IndustryName")
            }).ToList<Industry>(),

        // populate key skills
        KeySkill = dsContractor.Tables[2].AsEnumerable().Where(KeySkill => KeySkill.Field<int>("ContractorId") == contractor.Field<int>("Id")).
            Select(target => new KeySkill
            {
                intId = target.Field<int>("ContractorId"),
                strKeySkillName = target.Field<string>("KeySkillName")
            }).ToList<KeySkill>(),

        // populate email addresses
        EmailAddress = dsContractor.Tables[3].AsEnumerable().Where(EmailAddress => EmailAddress.Field<int>("ContractorId") == contractor.Field<int>("Id")).
            Select(target => new EmailAddress
            {
                intId = target.Field<int>("ContractorId"),
                strEmailAddress = target.Field<string>("EmailAddress"),
                strEmailType = target.Field<string>("EmailType")
            }).ToList<EmailAddress>(),

        // populate phone numbers
        PhoneNumber = dsContractor.Tables[4].AsEnumerable().Where(PhoneNumber => PhoneNumber.Field<int>("ContractorId") == contractor.Field<int>("Id")).
            Select(target => new PhoneNumber
            {
                intId = target.Field<int>("ContractorId"),
                strPhoneNumberType = target.Field<string>("PhoneType"),
                strPhoneNumber = target.Field<string>("PhoneNumber")
            }).ToList<PhoneNumber>(),

        Geography = dsContractor.Tables[5].AsEnumerable().Where(PhoneNumber => PhoneNumber.Field<int>("ContractorId") == contractor.Field<int>("Id")).
                Select(target => new Geography
                {
                    intId = target.Field<int>("ContractorId"),
                    strGeography = target.Field<string>("GeographyName")
                }).ToList<Geography>(),


    }).ToList<Contractor>();
    public int intContractorId;
public string strFirstName;
public string strMiddleName;
public string strLastName;
public string strDescription; 
public DateTime dtDOB;
public string strGender;
public string strProfile;
public int intIsFullTime;
public int intWorkMonday;
public int intWorkTuesday;
public int intWorkWednesday;
public int intWorkThursday;
public int intWorkFriday;
public int intWorkSaturday;
public int intWorkSunday;
public DateTime dtAvailableFrom;
public int intActive;
public decimal dcSubscrptionCost;
public DateTime dtRenewalDate;
public List<Address> Address;
public List<EmailAddress> EmailAddress;
public List<PhoneNumber> PhoneNumber;
public List<KeySkill> KeySkill;
public List<Geography> Geography;
public List<Industry> Industry;
public List<SubIndustry> SubIndustry;
public int intContractorId;
公共字符串strFirstName;
公共字符串strMiddleName;
公共字符串strLastName;
公共字符串描述;
公共日期时间dtDOB;
公共字符串字符串字符串;
公共字符串strProfile;
公共时间;
星期一公众假期;
星期二公众假期;
星期三公众假期;
公众谘询服务(星期四);;
星期五公众假期;
公众国际工作日;
公共国际工作日;
公共日期时间dtAvailableFrom;
公众参与;
公共十进制dcSubscrptionCost;
公共日期时间dtRenewalDate;
公开名单地址;
公开名单电邮地址;
公众电话号码;
公开列表击键;
公共列表地理;
上市行业;
公开上市子行业;
所讨论的字段是
strProfile
,但我一辈子都不明白为什么我会得到我现在的内容,而不是字段包含的字符串

我已经在调试模式下进行了检查,正确的数据在
数据集中

如果我遗漏了什么,请告诉我

谢谢这部分:

...Select(profile => profile.Field<string>("ContractorProfile")).ToString()

您正在使用
Where
选择行枚举,然后从每行中选择一个字符串,最后在可枚举项上调用
ToString()
。也许您打算使用
单个
而不是
其中

strProfile = dsContractor.Tables[7].AsEnumerable()
    .Single(profile => profile.Field<int>("ContractorId") == contractor.Field<int>("Id"))
    .Select(profile => profile.Field<string>("ContractorProfile")),
strProfile=dsContractor.Tables[7].AsEnumerable()
.Single(profile=>profile.Field(“ContractorId”)==contractor.Field(“Id”))
.选择(profile=>profile.Field(“ContractorProfile”),
您应该使用方法而不是
ToString()
,因为您的查询返回一个
IEnumerable

strProfile = dsContractor.Tables[7].AsEnumerable().Where(profile => profile.Field<int>("ContractorId") == contractor.Field<int>("Id")).
            Select(profile => profile.Field<string>("ContractorProfile")).SingleOrDefault();
strProfile=dsContractor.Tables[7].AsEnumerable().Where(profile=>profile.Field(“ContractorId”)==contractor.Field(“Id”)。
选择(profile=>profile.Field(“ContractorProfile”).SingleOrDefault();
编辑:

因为您使用了
ToString()
,所以我假设您确定查询返回1个元素。事实上,只有在完全确定查询返回的最大值为1个元素时,才应使用此解决方案