3

SQLから返されたフィールドとリストを含むlstContractorC#クラス()のリスト()にデータを入力しようとしています。IDでリンクされた複数のテーブルを返します。contractorDataSetDataSet

テーブルから必要な唯一のフィールドである1つのフィールドを除いて、すべてが機能するようになりました。LINQを使用して選択しようとすると、System.Data.EnumerableRowCollection代わりに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;

問題のフィールドはstrProfileですが、フィールドに含まれる文字列ではなく、自分が自分であるコンテンツを取得している理由を一生理解できません。

デバッグモードでチェックしましたが、正しいデータがにありDataSetます。

私が何かを逃したかどうか私に知らせてください。

ありがとう

4

3 に答える 3

2

この部分:

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

LinqSelect()はIEnumerableを返すので、それToString()がIEnumerableであることを伝えようとします。返されたコレクションの最初のアイテムが必要な場合は、First()またはFirstOrDefault():を使用します。

...Select(profile => profile.Field<string>("ContractorProfile")).First().ToString()
于 2012-10-24T16:22:01.133 に答える
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 に答える
1

クエリが:を返すため、のSingleOrDefault()代わりにメソッドを使用する必要があります。ToString()IEnumerable

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

編集:

使用ToString()したので、クエリが1つの要素を返すことは確かだと思いました。実際、このソリューションは、クエリが最大1つの要素を返すことが完全に確実である場合にのみ使用する必要があります。それ以外の場合は、例外が発生します。複数の結果が返される場合は、FirstOrDefault()メソッドを使用する必要があります。このメソッドは、最初の要素を返します。それ以外の場合は、デフォルトの型の値を返します。

于 2012-10-24T16:23:36.260 に答える