LINQ to Sharepoint InsertOnSubmit 问题

发布于 2024-10-20 04:18:07 字数 996 浏览 1 评论 0原文

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

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

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

我尝试过这个,但它返回一个错误指定的强制转换无效。

这是当前代码,

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

For example I have a list called Product and it has 3 columns, ProductName (which is the Title), ProductPrice and ProductType.

  • ProductName is a string
  • ProductPrice is a currency (double)
  • ProductType is a LookUp on ProductTypes List

Normally this is easy for me if it does not contain a LookUp column, but I dont know how to deal with look up columns when Inserting.

I had tried this one but it returns an error Specified cast is not valid.

Here is the current code

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

What would I do with the newProduct.ProductType as here is where the error occurs.

Please note that the ddProductType DataSource is the ProductType List and uses Title in its DataTextField and DataValueField

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

著墨染雨君画夕 2024-10-27 04:18:07

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

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 = "[email protected]",
    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();

This might help you out. The first example explains how the insert should work with links to existing data. This sample code should give you enough hints to help you fix your problem:

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 = "[email protected]",
    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();
聆听风音 2024-10-27 04:18:07

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

newProduct.ProductType = new FieldLookupValue { LookupId = 1 };

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

I'm not familiar with Linq to SharePoint, but I assume it is similar to the Client Object model. If so, you'll need to use a FieldLookupValue for the value of newProduct.ProductType and use the ID of the lookup as the value:

newProduct.ProductType = new FieldLookupValue { LookupId = 1 };

This means you'll need to have access to the lookup value's ListID in your ProductTypes query.

忆依然 2024-10-27 04:18:07

你的做法对我来说似乎是正确的。这也是我一直以来成功的做法。您确定问题出在查找列上吗? double 是正确的货币类型吗?货币通常保存为小数,而不是双精度。

顺便说一句,您不必单独获取实体列表。 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();

希望这有帮助

The way you are doing seems correct to me. It is the same way I've been doing it with success. Are you sure the problem is the lookup column? Is double the correct type for the currency? Often currency is saved as a decimal, not a double.

By the way, you don't have to get the Entitylist separately. SPMetal makes the shorthand dc.ProductType, like the one you already use in the insertOnSubmit. No idea why all examples do this...

Try to split the assignments a bit and debug again. See if everything is the way it should be.

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

Hope this helps

一个人的夜不怕黑 2024-10-27 04:18:07

它现在可以工作,这是解决方案

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< /strong>,然后将其转换为 ProductTypeItem 现在它可以工作了

It works now and here is the solution

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

The only thing I changed is to initialize the the Product Type as Item rather than ProductTypeItem, then cast it to ProductTypeItem now it works

浅紫色的梦幻 2024-10-27 04:18:07

LINQ to SharePoint 生成的代码与您的列表不同步。重新生成列表就可以了 -Paul Beck

The LINQ to SharePoint generated code is out of sync with you list. Re-generate the list and it will work -Paul Beck

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文