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
This part:
...Select(profile => profile.Field<string>("ContractorProfile")).ToString()
Linq's Select() returns an IEnumerable, so ToString() is just going to try to tell you that it's an IEnumerable. If you want the first item from the returned collection, use First() or FirstOrDefault():
...Select(profile => profile.Field<string>("ContractorProfile")).First().ToString()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With