[英]LINQ to Sharepoint InsertOnSubmit Question
例如,我有一個名為Product的列表,它具有3列,即ProductName(即標題),ProductPrice和ProductType。
通常,如果它不包含LookUp列,這對我來說很容易,但是我不知道插入時如何處理查找列。
我嘗試過此操作,但返回錯誤Specified cast is not valid.
這是當前代碼
EntityList<ProductTypeItem> ProductTypes = dc.GetList<ProductTypeItem>("ProductType");
ProductItem newProduct = new ProductItem();
newProduct.Title = txtProductName.Text;
newProduct.ProductPrice = double.Parse(txtProductPrice.Text);
newProduct.ProductType = (from a in ProductTypes where a.Title == ddProductType.SelectedItem.Text select a).FirstOrDefault();
dc.Product.InsertOnSubmit(newProduct);
dc.SubmitChanges();
我將如何處理newProduct.ProductType
因為這是發生錯誤的地方。
請注意,ddProductType數據源是ProductType列表,並在其DataTextField
和DataValueField
使用Title
這可能會幫助您。 第一個示例說明了插入如何與現有數據的鏈接一起使用。 此示例代碼應為您提供足夠的提示,以幫助您解決問題:
AdventureWorksDataContext db = new AdventureWorksDataContext();
// LINQ query to get StateProvince
StateProvince state = (from states in db.StateProvinces
where states.CountryRegionCode == "AU" && states.StateProvinceCode == "NSW"
select states).FirstOrDefault();
// LINQ function to get AddressType
AddressType addrType = db.AddressTypes.FirstOrDefault(s => s.Name == "Home");
Customer newCustomer = new Customer()
{
ModifiedDate= DateTime.Now,
AccountNumber= "AW12354",
CustomerType='I',
rowguid= Guid.NewGuid(),
TerritoryID= state.TerritoryID // Relate record by Keys
};
Contact newContact = new Contact()
{
Title = "Mr",
FirstName = "New",
LastName = "Contact",
EmailAddress = "newContact@company.com",
Phone = "(12) 3456789",
PasswordHash= "xxx",
PasswordSalt= "xxx",
rowguid = Guid.NewGuid(),
ModifiedDate = DateTime.Now
};
Individual newInd = new Individual()
{
Contact= newContact, // Relate records by objects (we dont actually know the Keys for the new records yet)
Customer= newCustomer,
ModifiedDate= DateTime.Now
};
Address newAddress = new Address()
{
AddressLine1= "12 First St",
City= "Sydney",
PostalCode= "2000",
ModifiedDate=DateTime.Now,
StateProvince= state,
rowguid = Guid.NewGuid()
};
// Link our customer with their address via a new CustomerAddress record
newCustomer.CustomerAddresses.Add(new CustomerAddress() { Address = newAddress, Customer = newCustomer, AddressType = addrType, ModifiedDate = DateTime.Now, rowguid = Guid.NewGuid() });
// Save changes to the database
db.SubmitChanges();
我對SharePoint的Linq不熟悉,但我認為它與客戶端對象模型相似。 如果是這樣,則需要將FieldLookupValue用作newProduct.ProductType的值, newProduct.ProductType
查找的ID
用作值:
newProduct.ProductType = new FieldLookupValue { LookupId = 1 };
這意味着您將需要在ProductTypes
查詢中訪問查找值的ListID
。
你的做法對我來說似乎是正確的。 這與我成功取得成功的方式相同。 您確定問題出在查找列嗎? double是貨幣的正確類型嗎? 通常貨幣會保存為小數,而不是雙精度。
順便說一句,您不必單獨獲取Entitylist。 SPMetal生成dc.ProductType的簡寫,就像您在insertOnSubmit中已經使用的那樣。 不知道為什么所有示例都會這樣做...
嘗試稍微分配一下分配,然后再次調試。 看看是否一切都應該如此。
ProductItem newProduct = new ProductItem();
string selectedProductType = ddProductType.SelectedItem.Text;
ProductTypeItem productType = (from a in dc.ProductType
where a.Title == selectedProductType
select a).FirstOrDefault();
newProduct.Title = txtProductName.Text;
newProduct.ProductPrice = decimal.Parse(txtProductPrice.Text);
newProduct.ProductType = productType;
dc.Product.InsertOnSubmit(newProduct);
dc.SubmitChanges();
希望這可以幫助
現在可以使用,這是解決方案
EntityList<Item> ProductTypes = dc.GetList<Item>("ProductType");
Item oProductType = (from a in ProductTypes where a.Title == ddProductType.SelectedItem.Text select a).FirstOrDefault();
ProductItem newProduct = new ProductItem();
newProduct.Title = txtProductName.Text;
newProduct.ProductPrice = double.Parse(txtProductPrice.Text);
newProduct.ProductType = (ProductTypeItem)oProductType;
dc.Product.InsertOnSubmit(newProduct);
dc.SubmitChanges();
我唯一更改的是將產品類型初始化為Item
而不是ProductTypeItem
,然后將其強制轉換為ProductTypeItem
LINQ to SharePoint生成的代碼與您的列表不同步。 重新生成列表,它將起作用-Paul Beck
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.