首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >LINQ to Sharepoint InsertOnSubmit问题

LINQ to Sharepoint InsertOnSubmit问题
EN

Stack Overflow用户
提问于 2011-03-02 22:17:51
回答 5查看 2.6K关注 0票数 2

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

  • ProductName是一个字符串
  • ProductPrice是一种货币(双倍)
  • ProductType是ProductTypes列表上的一个LookUp

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

我试过了,但是它返回了一个错误Specified cast is not valid.

下面是当前的代码

代码语言:javascript
复制
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,在DataTextFieldDataValueField中使用Title

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2011-03-03 20:11:38

它现在起作用了,这就是解决办法。

代码语言:javascript
复制
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,现在它可以工作了

票数 0
EN

Stack Overflow用户

发布于 2011-03-02 23:30:39

也许能帮你解决问题。第一个示例说明如何使用指向现有数据的链接进行插入。这个示例代码应该为您提供足够的提示,帮助您解决问题:

代码语言:javascript
复制
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();
票数 1
EN

Stack Overflow用户

发布于 2011-03-02 23:41:23

我不熟悉Linq到SharePoint,但我假设它类似于客户端对象模型。如果是这样的话,您需要使用FieldLookupValue作为newProduct.ProductType的值,并使用查找的ID作为值:

代码语言:javascript
复制
newProduct.ProductType = new FieldLookupValue { LookupId = 1 };

这意味着您需要在ListID查询中访问查找值的ProductTypes

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5174225

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档