문제

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?

도움이 되었습니까?

해결책

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.

다른 팁

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();
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top