LINQ to Sharepoint InsertOnSubmit 问题
例如,我有一个名为 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,并在其 DataTextField
和 DataValueField
中使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这可能会帮助您。第一个示例解释了插入应如何与现有数据的链接一起使用。此示例代码应该为您提供足够的提示来帮助您解决问题:
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:
我不熟悉 Linq to SharePoint,但我认为它是类似于客户端对象模型。如果是这样,您需要使用 FieldLookupValue 获取
newProduct.ProductType
的值,并使用查找的ID
作为值:这意味着您需要有权访问查找值的
。ProductTypes
查询中的 ListIDI'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 theID
of the lookup as the value:This means you'll need to have access to the lookup value's
ListID
in yourProductTypes
query.你的做法对我来说似乎是正确的。这也是我一直以来成功的做法。您确定问题出在查找列上吗? double 是正确的货币类型吗?货币通常保存为小数,而不是双精度。
顺便说一句,您不必单独获取实体列表。 SPMetal 制作了简写 dc.ProductType,就像您在 insertOnSubmit 中使用的那样。不知道为什么所有示例都这样做...
尝试稍微拆分分配并再次调试。看看一切是否都按其应有的方式进行。
希望这有帮助
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.
Hope this helps
它现在可以工作,这是解决方案
我唯一更改的是将产品类型初始化为
Item
而不是ProductTypeItem
< /strong>,然后将其转换为 ProductTypeItem 现在它可以工作了It works now and here is the solution
The only thing I changed is to initialize the the Product Type as
Item
rather thanProductTypeItem
, then cast it to ProductTypeItem now it worksLINQ 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