我們有一個現有的數據庫和一個腳本,它會接收一封電子郵件,找到我們想要的數據片段(即名稱,地址,電子郵件等),然後將數據插入數據庫作爲字段/值對(「FieldLabel」的列)「FieldValue」的&)。在WCF服務中迭代數據成員
我現在正在實現一個API(WCF服務)來取代使用C#與Entity Framework的電子郵件解析。我正在創建一個DataContract,「Details」,它包含傳遞給API的「Name」,「Address」,「Email」等DataMemebers。然後我有一個InsertData()方法,我想用它來獲取每個數據成員並將其名稱用作FieldLabel,並將其值作爲FieldValue。例如,在Name =「Brian」和ZipCode =「11111」的情況下,我想要創建Name/Brian & ZipCode/11111的標籤/值對。
這裏就是我以後的僞代碼:
DetailsTable _detail = new DetailsTable(); // EF table object
foreach (datamemeber in Details)
{
_detail.FieldLabel = datamemeber.toString();
_detail.FieldValue = datamember.Value();
}
更新:加入了代碼爲我詳細DataContract
[DataContract]
public class Details
{
public LeadDetail()
{
FirstName = "";
LastName = "";
StreetAddress = "";
City = "";
State = "";
ZipCode = "";
}
public LeadDetail(string firstName, string lastName, string streetAddress,
string city, string state, string zipCode)
{
FirstName = firstName;
LastName = lastName;
StreetAddress = streetAddress;
City = city;
State = state;
ZipCode = zipCode;
}
[DataMember]
public string FirstName;
[DataMember]
public string LastName;
[DataMember]
public string StreetAddress;
[DataMember]
public string City;
[DataMember]
public string State;
[DataMember]
public string ZipCode;
}
當我實現這個代碼時,它抱怨「Details is a'type'',但像'variable'一樣使用。我已將DataContract for Details添加到我的原始帖子中。 – BrianKE 2012-02-09 14:21:21
對不起,'foreach'應該是'foreach(詳細datamember)',以便您正在迭代的細節是集合的實際實例,而不是集合的類型。 – 2012-02-09 16:27:11