0

浏览了相关的帖子,但仍然有困难。这是我的代码:

var id = Request.QueryString["id"].AsInt();
var accountNum = Request.QueryString["accountnum"];

var orderIDs = new List<OrderID>();

// Order IDs are in the form: OrderNum-OrderLine[,...]
// e.g. orderids=41417-6,36703-1

foreach (var orderID in Request.QueryString["orderids"].Split(',')) {
    var components = orderID.Split('-');
    orderIDs.Add(new OrderID() {
        OrderNum = components[0].AsInt(), 
        OrderLine = components[1].AsInt()
    });
}

var dbContext = new TelecomEntities();
var assignedLines = dbContext.LineAssignments
    .Where(a => orderIDs
        .Any(i => a.OrderNum == i.OrderNum && a.OrderLine == i.OrderLine))
    .ToList();

运行此程序时,出现错误:无法创建“OrderID”类型的常量值。此上下文仅支持原始类型或枚举类型。

根据我在其他帖子中阅读的内容,我无法执行查询,因为orderIDs不在dbContent上下文中。

编辑: OrderID 类

public class OrderID
{
    public int OrderNum { get; set; }
    public int OrderLine { get; set; }

    public OrderID() { }
}
4

2 回答 2

0

我打赌它是 AsInt(我以前从未见过,但我看到它是网页的一部分)。

你在foreach中得到错误吗?如果是这样,试试这个:

        foreach (var orderID in testOrderIDs.Split(','))
        {
            var components = orderID.Split('-');
            orderIDs.Add(new OrderID()
            {
                OrderNum = int.Parse(components[0]),
                OrderLine = int.Parse(components[1])
            });
        }
于 2013-06-27T21:41:51.337 回答
0

问题是 EF 无法将您对 OrderID 对象的比较转换为 SQL。一种选择可能是将订单 ID 保留为字符串:

var id = Request.QueryString["id"].AsInt();
var accountNum = Request.QueryString["accountnum"];

string[] orderIDs;

// Order IDs are in the form: OrderNum-OrderLine[,...]
// e.g. orderids=41417-6,36703-1

orderIDs = Request.QueryString["orderids"].Split(',');

var dbContext = new TelecomEntities();
var assignedLines = dbContext.LineAssignments
    .Where(a => orderIDs
        .Any(on => on.Equals(a.OrderNum + "-" + a.OrderLine))
    .ToList();
于 2013-06-27T21:46:29.427 回答