p.campbell
p.campbell

Reputation: 100607

AttachAll exception: type arguments for method cannot be inferred from the usage. Try specifying the type arguments explicitly

Scenario: Trying to call the .AttachAll method on a table in my LinqToSql DataContext object.

Here's the relevant simplified snippet:

  public void Update(Customer cust){
    MyDataContext db = new MyDataContext();            
    db.CustomerInvoices.AttachAll(cust.Invoices);  //exception raised here!
    db.Customer.Attach(cust);
  }

Exception raised by the Compiler:

The type arguments for method 'System.Data.Linq.Table(Invoices).AttachAll(TSubEntity)(System.Collections.Generic.IEnumerable(TSubEntity))' cannot be inferred from the usage. Try specifying the type arguments explicitly.

Question: What is the proper way to cast the collection properly? Any other solutions besides a cast?

Upvotes: 1

Views: 1117

Answers (3)

Shahzad Ali
Shahzad Ali

Reputation: 21

public string Save(Role objRole)
{
    string message = string.Empty;
    System.Data.Common.DbTransaction trans = null;    
    try
    {
        Objdb.Connection.Open();
        trans = Objdb.Connection.BeginTransaction();
        Objdb.Transaction = trans; 
        if (objRole.RoleId == 0)
        {
            Objdb.Roles.InsertOnSubmit(objRole);
        }
        else
        {                  
            Objdb.Roles.Attach(objRole, true);
            Objdb.RolePages.AttachAll(objRole.RolePages.Where(a => a.IsNew == false && a.IsDeleted == false), true);
            Objdb.RolePages.InsertAllOnSubmit(objRole.RolePages.Where(a => a.IsNew == true && a.IsDeleted == false));
            Objdb.RolePages.DeleteAllOnSubmit(objRole.RolePages.Where(a => a.IsNew == false && a.IsDeleted == true));
        }
        Objdb.SubmitChanges();
        trans.Commit();  
        message = "Record saved successfully.";
    }
    catch (Exception ex)
    {
        trans.Rollback();
        message = "Error : " + ex.Message;
    }
    return message; 
}

Upvotes: 0

Shahzad Ali
Shahzad Ali

Reputation: 21

        else if (((CheckBox)item.Cells[2].FindControl("ckbSelect")).Checked == true && ((Label)item.Cells[2].FindControl("lblIsInuse")).Text == "1")
        {
            RolePage objTemp = new RolePage();
            objTemp = new Helper().GetRolePagebyID(roleId, Convert.ToInt32(item.Cells[0].Text));
            rp.RoleId = objTemp.RoleId;
            rp.PageId = objTemp.PageId;
            rp.RolePageId = objTemp.RolePageId;
            rp.CreatedOn = objTemp.CreatedOn;
            rp.Timestamp = objTemp.Timestamp; 
            //rp.RoleId = roleId;
            //rp.PageId = Convert.ToInt32(item.Cells[0].Text);
            //rp.RolePageId =Convert.ToInt32(((Label)item.Cells[2].FindControl("lblRolePageId")).Text.Trim());  
            rp.IsNew = false;
            rp.IsDeleted = false;                
            rp.UpdatedOn = DateTime.Now;  
            erp.Add(rp);               
        }

Upvotes: 0

Dave
Dave

Reputation: 4412

Tf cust.Invoices already refers to instances of the CustomerInvoices table, just doing db.Customers.Attach(cust); db.Update(); should be all you need to do.

If CustomerInvoices is a different type from Customer.Invoice, you'll probably need to iterate through the collection, and cast each one.

Upvotes: 1

Related Questions