简体   繁体   English

LINQ to Sharepoint InsertOnSubmit问题

[英]LINQ to Sharepoint InsertOnSubmit Question

For example I have a list called Product and it has 3 columns, ProductName (which is the Title), ProductPrice and ProductType. 例如,我有一个名为Product的列表,它具有3列,即ProductName(即标题),ProductPrice和ProductType。

  • ProductName is a string ProductName是一个字符串
  • ProductPrice is a currency (double) ProductPrice是一种货币(双精度)
  • ProductType is a LookUp on ProductTypes List ProductType是ProductTypes列表上的查找

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. 通常,如果它不包含LookUp列,这对我来说很容易,但是我不知道插入时如何处理查找列。

I had tried this one but it returns an error Specified cast is not valid. 我尝试过此操作,但返回错误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. 我将如何处理newProduct.ProductType因为这是发生错误的地方。

Please note that the ddProductType DataSource is the ProductType List and uses Title in its DataTextField and DataValueField 请注意,ddProductType数据源是ProductType列表,并在其DataTextFieldDataValueField使用Title

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

I'm not familiar with Linq to SharePoint , but I assume it is similar to the Client Object model. 对SharePoint的Linq不熟悉,但我认为它与客户端对象模型相似。 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: 如果是这样,则需要将FieldLookupValue用作newProduct.ProductType的值, newProduct.ProductType查找的ID用作值:

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

This means you'll need to have access to the lookup value's ListID in your ProductTypes query. 这意味着您将需要在ProductTypes查询中访问查找值的ListID

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? double是货币的正确类型吗? Often currency is saved as a decimal, not a double. 通常货币会保存为小数,而不是双精度。

By the way, you don't have to get the Entitylist separately. 顺便说一句,您不必单独获取Entitylist。 SPMetal makes the shorthand dc.ProductType, like the one you already use in the insertOnSubmit. SPMetal生成dc.ProductType的简写,就像您在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 希望这可以帮助

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 我唯一更改的是将产品类型初始化为Item而不是ProductTypeItem ,然后将其强制转换为ProductTypeItem

The LINQ to SharePoint generated code is out of sync with you list. LINQ to SharePoint生成的代码与您的列表不同步。 Re-generate the list and it will work -Paul Beck 重新生成列表,它将起作用-Paul Beck

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM