Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# LINQ到Sharepoint InsertOnSubmit问题_C#_Linq_Sharepoint_Linq To Sharepoint - Fatal编程技术网

C# LINQ到Sharepoint InsertOnSubmit问题

C# LINQ到Sharepoint InsertOnSubmit问题,c#,linq,sharepoint,linq-to-sharepoint,C#,Linq,Sharepoint,Linq To Sharepoint,例如,我有一个名为Product的列表,它有3列:ProductName(即标题)、ProductPrice和ProductType ProductName是一个字符串 ProductPrice是一种货币(双倍) ProductType是对ProductTypes列表的查找 通常,如果它不包含查找列,这对我来说很容易,但我不知道在插入时如何处理查找列 我尝试过这个,但它返回一个错误指定的强制转换无效。 这是当前的代码 EntityList<ProductTypeItem> Pro

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

  • ProductName是一个字符串
  • ProductPrice是一种货币(双倍)
  • ProductType是对ProductTypes列表的查找
通常,如果它不包含查找列,这对我来说很容易,但我不知道在插入时如何处理查找列

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

这是当前的代码

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();   
EntityList ProductType=dc.GetList(“ProductType”);
ProductItem newProduct=新ProductItem();
newProduct.Title=txtProductName.Text;
newProduct.ProductPrice=double.Parse(txtProductPrice.Text);
newProduct.ProductType=(从ProductTypes中的a开始,其中a.Title==ddProductType.SelectedItem.Text选择a);
dc.Product.InsertOnSubmit(新产品);
dc.提交更改();
我将如何处理
newProduct.ProductType
,因为这里是发生错误的地方

请注意,ddProductType数据源是产品类型列表,在其
数据文本字段
中使用
标题
,而
数据值字段
可能会帮到您。第一个示例解释了insert应该如何处理指向现有数据的链接。此示例代码应为您提供足够的提示,以帮助您解决问题:

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();
我不熟悉,但我假设它类似于客户机对象模型。如果是这样,您需要使用a作为
newProduct.ProductType
的值,并使用查找的
ID
作为值:

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

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

我认为您的做法是正确的。这和我成功地做到这一点是一样的。您确定问题出在查找列上吗?double是货币的正确类型吗?货币通常保存为十进制,而不是双精度

顺便说一下,您不必单独获取Entitylist。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();

希望这能有所帮助,它现在可以工作了,下面是解决方案

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();   
EntityList ProductType=dc.GetList(“ProductType”);
Item oProductType=(来自ProductTypes中的a,其中a.Title==ddProductType.SelectedItem.Text选择a);
ProductItem newProduct=新ProductItem();
newProduct.Title=txtProductName.Text;
newProduct.ProductPrice=double.Parse(txtProductPrice.Text);
newProduct.ProductType=(ProductTypeItem)opProductType;
dc.Product.InsertOnSubmit(新产品);
dc.提交更改();

我唯一更改的是将产品类型初始化为
,而不是
ProductTypeItem
,然后将其转换为ProductTypeItem现在可以工作了LINQ to SharePoint生成的代码与您的列表不同步。重新生成列表,它会工作的-Paul Beck

这看起来像是要去某个地方了,我使用newProduct.ProductType=new SPFieldLookupValue{LookupId=1}尝试了这个方法;但是现在有一个不同的错误,因为ProductType是ProductTypeItem类型,所以它的抛出“不能隐式地将类型‘Microsoft.SharePoint.SPFieldLookupValue’转换为‘ProductTypeItem’是它的查找列,如果我将null传递给该列,它工作正常。