3

I'm trying to populate a list (lstContractor) of a C# class (contractor), containing fields and lists, from a DataSet returned from SQL. The DataSet returns multiple tables, linked by an id.

I've managed to get it all working except 1 field, which is the only field I need from the table it is in. When I'm trying to select it using LINQ, I'm getting System.Data.EnumerableRowCollection1[System.String]` instead of the contents of the field.

My Code looks like this:

    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>();

the class looks like this:

    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;

The field in question is strProfile, but I can't for the life of me figure out why I'm getting the contents that I am, instead of the string the the field contains.

I've checked in debug mode, and the correct data is in the DataSet.

Please let me know if I've missed anything.

Thanks

4

3 回答 3

2

这部分:

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

LinqSelect()返回一个 IEnumerable,所以ToString()只是试图告诉你它是一个 IEnumerable。如果您想要返回集合中的第一项,请使用First()or FirstOrDefault()

...Select(profile => profile.Field<string>("ContractorProfile")).First().ToString()
于 2012-10-24T16:22:01.133 回答
1

You should use SingleOrDefault() method instead of ToString(), becuase your query returns an IEnumerable:

strProfile = dsContractor.Tables[7].AsEnumerable().Where(profile => profile.Field<int>("ContractorId") == contractor.Field<int>("Id")).
            Select(profile => profile.Field<string>("ContractorProfile")).SingleOrDefault();

EDIT:

Becuase you used ToString() I assumed you was sure that the query returns 1 element. Infact you should use this solution only if you're completely sure that your query returns a maximum of 1 element else it gives an excpetion. If it returns more than one result you should use FirstOrDefault() method which returns the first element else the default type value.

于 2012-10-24T16:23:36.260 回答
1

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

strProfile = dsContractor.Tables[7].AsEnumerable()
    .Single(profile => profile.Field<int>("ContractorId") == contractor.Field<int>("Id"))
    .Select(profile => profile.Field<string>("ContractorProfile")),
于 2012-10-24T16:22:06.093 回答