0

您好,我刚刚开始尝试了解 Parallel Linq,但我的第一次尝试没有成功。我正在使用 EF 4.0 和我创建的用于查询数据的存储库模式类。我不认为存储库模式是问题,但我可能弄错了。

我拥有的数据库没有按照我想要的方式设置,但是我继承了系统。我遇到问题的代码如下:

            var gId = Sql.ToGuid(Request["ID"]);

            var lOrdersGridList = new OrdersGridList(); //Class that only contains properties

            var lOrdersForContact = new BaseRepository<ORDER>()
                .Find(i => i.ORDERS_CONTACTS.Where(b => b.CONTACT_ID == gId).Count() > 0).AsParallel()
                .Select(i =>
                    new OrdersGridList
                        {
                            ORDER_ID = i.ID,
                            ORDER_NUM = i.ORDER_NUM,
                            SHIPPING_ACCOUNT_ID = i.ORDERS_ACCOUNTS.Where(b =>  b.ORDER_ID == i.ID && b.ACCOUNT_ROLE == "Ship To").First().ACCOUNT_ID,
                            SHIPPING_ACCOUNT_NAME = i.ORDERS_ACCOUNTS.Where(b => b.ORDER_ID == i.ID && b.ACCOUNT_ROLE == "Ship To").First().ACCOUNT.NAME,
                            SHIPPING_CONTACT_ID = i.ORDERS_CONTACTS.Where(b => b.ORDER_ID == i.ID && b.CONTACT_ROLE == "Ship To").First().CONTACT_ID,
                            SHIPPING_CONTACT_NAME = i.ORDERS_CONTACTS.Where(b => b.ORDER_ID == i.ID && b.CONTACT_ROLE == "Ship To")
                                                                .Select(b => new { SHIPPING_CONTACT_NAME = (b.CONTACT.FIRST_NAME + ' ' + b.CONTACT.LAST_NAME) }).First().SHIPPING_CONTACT_NAME,
                            NAME = i.NAME

                        }).DefaultIfEmpty(lOrdersGridList).ToList<OrdersGridList>();


            grdMain.DataSource = lOrdersForContact.ToDataTable().DefaultView; //ToDataTable extension function converts the List Object to a datatable.

如果我在没有 AsParallel 的情况下运行代码,则代码执行没有问题,但是一旦添加 AsParallel,我会收到以下错误:

错误信息

另外,以防万一您想看到这是我为 Select Above 声明为新的类:

public class OrdersGridList : EntityObject
{
    public string ORDER_NUM { get; set; }

    public Guid ORDER_ID { get; set; }

    public Guid SHIPPING_ACCOUNT_ID { get; set; }

    public string SHIPPING_ACCOUNT_NAME { get; set; }

    public Guid SHIPPING_CONTACT_ID { get; set; }

    public string SHIPPING_CONTACT_NAME { get; set; }

    public string NAME { get; set; }
}

如果我删除了用于在选择中检索数据的所有关系,我不会收到任何错误:

                var lOrdersForContact = new BaseRepository<ORDER>()
                .Find(i => i.ORDERS_CONTACTS.Where(b => b.CONTACT_ID == gId).Count() > 0).AsParallel()
                .Select(i =>
                    new OrdersGridList
                        {
                            ORDER_ID = i.ID,
                            ORDER_NUM = i.ORDER_NUM,
                            //SHIPPING_ACCOUNT_ID = i.ORDERS_ACCOUNTS.Where(b =>  b.ORDER_ID == i.ID && b.ACCOUNT_ROLE == "Ship To").First().ACCOUNT_ID,
                            //SHIPPING_ACCOUNT_NAME = i.ORDERS_ACCOUNTS.Where(b => b.ORDER_ID == i.ID && b.ACCOUNT_ROLE == "Ship To").First().ACCOUNT.NAME,
                            //SHIPPING_CONTACT_ID = i.ORDERS_CONTACTS.Where(b => b.ORDER_ID == i.ID && b.CONTACT_ROLE == "Ship To").First().CONTACT_ID,
                            //SHIPPING_CONTACT_NAME = i.ORDERS_CONTACTS.Where(b => b.ORDER_ID == i.ID && b.CONTACT_ROLE == "Ship To")
                            //                                    .Select(b => new { SHIPPING_CONTACT_NAME = (b.CONTACT.FIRST_NAME + ' ' + b.CONTACT.LAST_NAME) }).First().SHIPPING_CONTACT_NAME,
                            NAME = i.NAME

                        }).DefaultIfEmpty(lOrdersGridList).ToList<OrdersGridList>();

如果需要,我将非常乐意提供更多信息。您可以提供有关使用 Plinq 的任何帮助,我将不胜感激。

4

1 回答 1

0

在我看来,该类使用and参数BaseRepository创建了某种 LINQ to Entities 查询。FindSelect

UsingAsParellel用于 LINQ to Objects,您的代码在其中实际评估您传递的表达式。其他 LINQ 方言,包括 LINQ to Entities,将其翻译成不同的查询语言,如 SQL。SQL 服务器可以自己进行合理的并行化。

于 2012-11-08T17:00:57.720 回答