gpt4 book ai didi

c# - LINQ to Sharepoint InsertOnSubmit 问题

转载 作者:太空宇宙 更新时间:2023-11-03 14:21:39 25 4
gpt4 key购买 nike

例如,我有一个名为 Product 的列表,它有 3 列,ProductName(即标题)、ProductPrice 和 ProductType。

  • ProductName 是一个字符串
  • ProductPrice 是一种货币( double )
  • ProductType 是对 ProductTypes 列表的查找

通常,如果它不包含查找列,这对我来说很容易,但我不知道如何在插入时处理查找列。

我试过这个,但它返回错误 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 List 并在其 DataTextFieldDataValueField 中使用 Title/p>

最佳答案

This 可能会帮助您。第一个示例说明插入应如何与现有数据的链接一起使用。此示例代码应为您提供足够的提示来帮助您解决问题:

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();

关于c# - LINQ to Sharepoint InsertOnSubmit 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5174225/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com