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

标签 c# linq lambda

我正在尝试从 DataSet 填充 C# 类 (contractor) 的列表 (lstContractor),其中包含字段和列表> 从 SQL 返回。 DataSet 返回多个表,由一个 id 链接。

除 1 个字段外,我已经设法使它全部正常工作,这是我需要从它所在的表中获取的唯一字段。当我尝试使用 LINQ 选择它时,我得到 System. Data.EnumerableRowCollection1[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;

有问题的字段是 strProfile,但我终究无法弄清楚为什么我得到的是我现在的内容,而不是该字段包含的字符串。

我已经在 Debug模式下检查过,正确的数据在 DataSet 中。

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

谢谢

最佳答案

这部分:

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

Linq 的 Select() 返回一个 IEnumerable,因此 ToString() 只是试图告诉您它是一个 IEnumerable。如果您想要返回集合中的第一项,请使用 First()FirstOrDefault():

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

关于c# - 使用 linq lambda 从具有多个链接表的数据集中填充 C# 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13053280/

相关文章:

java - 在 Java 8 中使用 Memoized 的无限斐波那契数列

c# - 使用 gdi+ (C#) 将 png 转换为 gif

c# - 为什么 Process.Exited 事件会触发两次?

c# - 如何接受<>指定另一个键?或者更快的方法来区分两个巨大的 List<>?

linq - 将 lambda 表达式转换为表达式树

java - 创建一个方法,该方法接受可能具有不同类型的可变长度的 Function 参数

c# - Azure AppService 将Word转换为PDF

c# - 在密封类上实现 IDisposable

C# LINQ 用于类的面向树的数据结构

c# - 将带有句点的对象列表组合成唯一列表