0

I have two lists as below:

var processedOrders = this._requestReviewRecordService.GetAll().ToList();
var orders = _orderRepository.Table.Where(o => o.OrderStatusId == (int)OrderStatus.Complete && o.CreatedOnUtc < EntityFunctions.AddMinutes(DateTime.Now, -minutes)).ToList();

The lists are of different types, but both contain a property called OrderId.

Essentially I want to filter the second list "orders" of any records that a matching OrderId.

I've tried Linq's Except method, but this seems to only play nicely with primitive types.

Can anyone point me in the right direction - I didnt think this would be quite so challenging!

Thanks in advance Al

4

1 回答 1

0

这是一个执行您所追求的示例:

public class TypeOne
    {
        public int OrderId { get; set; }
        public string SomeOtherField { get; set; }
    }

    public class TypeTwo
    {
        public int OrderId { get; set; }
        public string MainField { get; set; }
    }


    class Program
    {
        static void Main(string[] args)
        {
            // A little bit of setup
            var first = new List<TypeOne>() { new TypeOne { OrderId = 1, SomeOtherField = "One" }, new TypeOne { OrderId = 2,  SomeOtherField = "Two" } } ;
            var second = new List<TypeTwo>() { new TypeTwo { OrderId = 1, MainField = "One" }, new TypeTwo { OrderId = 2, MainField = "Two" }, new TypeTwo { OrderId = 3, MainField = "Buckle" }, new TypeTwo { OrderId = 4, MainField = "MyShoe" } };

            // Here's where we do the interesting bit
            var firstIds = from id in first
                           select id.OrderId;

            var query = from item in second
                        where firstIds.Contains(item.OrderId)
                        select item;


            // And some boring results
            foreach (var i in query)
            {
                Console.WriteLine(string.Format("[OrderId: {0}, MainField: {1}]", i.OrderId, i.MainField));
            }

            Console.ReadLine();

        }

请记住,此示例使用简单列表作为对象的存储。如果您在任何查询中使用 Entitiy Framework EntitySet 作为源集合,则此方法并不总是有效,因为它们没有实现适用于 EF 的 Contains<>() 扩展方法。话虽如此,您可以先将您的查询具体化到列表中,然后这种方法就会起作用。

于 2013-05-10T00:29:03.573 回答