Pergunta

I am grabbing a value and want it to appear in the BatchId of every anonymous type created via a linq statement.

Here is the code:

    var batchId = context.Request["batchid"];
    using (var db = new StarterSiteEntities())
    {   // Get data
        var transactions = (from t in db.Transactions
                            join td in db.TransactionDetails on t.TransactionID equals td.TransactionID
                            join p in db.Products on td.ProductID equals p.ProductID
                            where t.Exported == false
                            select new
                            {
                                BatchId = batchId,
                                t.FirstName,
                                t.LastName,
                                t.Address1,
                                t.Address2,
                                t.City,
                                t.State,
                                t.Zip_Code,
                                t.Email,
                                t.Phone,
                                t.TotalAmount,
                                t.MonthlyGift,
                                t.DateCreated,
                                p.Fund,
                                ProductFirstName = p.FirstName,
                                ProductLastName = p.LastName,
                                ProductUniversity = p.University,
                                ProductState = p.State,
                                ProductEmail = p.Email,
                                ProductAmount = td.Amount
                            }).ToList();
    }

When I do this, I get the error message:

"A parameter is not allowed in this location. Ensure that the '@' sign is in a valid location or that parameters are valid at all in this SQL statement."

How do I reference the batchId variable from within the anonymous type declaration, or should I accomplish this another way?

Foi útil?

Solução

It looks like you ran into a known bug in the SQL Server CE data access libraries. You should be able to fix it by applying this hotfix to the machine(s) that are accessing the database.

Outras dicas

While I think Adam Maras answered my question. Because I did not want to install a hot-fix on the server, I ended up solving the problem using a different method.

Since the Linq query would not allow me to use a string variable and I could not edit the property value of an anonymous type. I stopped using an anonymous type and created an entity class to hold my "transaction summary" data.

Once I have a collection of TransactionSummary objects, I can use the Select() method to update the BatchId property value in each record.

Here is the resulting code:

// Define a custom type to hold the data
private class TransactionSummary
{
    public string BatchId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    //...removed lines for brevity...
}

//...here is the updated code snippet...
using (var db = new StarterSiteEntities())
{   // Get data
    var transactions = (from t in db.Transactions
                        join td in db.TransactionDetails on t.TransactionID equals td.TransactionID
                        join p in db.Products on td.ProductID equals p.ProductID
                        where t.Exported == false
                        select new TransactionSummary
                        {
                            FirstName = t.FirstName,
                            LastName = t.LastName,
                            //...removed lines for brevity...
                        }).ToList();

    // The client would like a batchID added to each record that we return.
    var batchId = context.Request["batchid"];
    transactions.Select(t => { t.BatchId = batchId; return t; }).ToList();
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top