例如,我有一个名为ProductPrice的列表,它有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 DataSource是ProductType List,在DataTextField和DataValueField中使用Title
发布于 2011-03-03 20:11:38
它现在起作用了,这就是解决办法。
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,现在它可以工作了
发布于 2011-03-02 23:30:39
这也许能帮你解决问题。第一个示例说明如何使用指向现有数据的链接进行插入。这个示例代码应该为您提供足够的提示,帮助您解决问题:
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();发布于 2011-03-02 23:41:23
我不熟悉Linq到SharePoint,但我假设它类似于客户端对象模型。如果是这样的话,您需要使用FieldLookupValue作为newProduct.ProductType的值,并使用查找的ID作为值:
newProduct.ProductType = new FieldLookupValue { LookupId = 1 };这意味着您需要在ListID查询中访问查找值的ProductTypes。
https://stackoverflow.com/questions/5174225
复制相似问题