LINQ:选择所有与单个字段匹配的内容,并在原始字段上合并 [英] LINQ : select all that match a single field, merge on original

查看:265
本文介绍了LINQ:选择所有与单个字段匹配的内容,并在原始字段上合并的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据另一个问题,我的问题进一步扩大了.我具有以下对象形状...(我正在使用RavenDB,这就是为什么我使用字符串"表示身份的原因)

Persuant to another question, my problem has extended a bit further. I have the following object shape ... ( I am using RavenDB, which is why I have 'strings' for identity)

这听起来并不像我说的那么复杂,但是为了简短起见很难交流,所以请耐心等待.如果这没有任何意义,我将尝试对其进行修改.

This is not as complicated as I make it sound, but it is hard to communicate in brevity, so bear with me. If this makes no sense I will try to revise it.

Collection A具有要获取的项目,而没有获取Collection B中具有匹配ID的项目.这两个集合不包含相同的数据类型.

Collection A has items I want to get, without getting the items in Collection B that have a matching ID. The two collections do not contain the same data type.

class Entity { 
  string Id { get; set; }
}
interface IName {
  string Name { get; set; }
}

class Reference<T> where T : Entity, IName {
  public string Id { get; set; }
        public string Name { get; set; }

        public static implicit operator Reference<T>(T doc) {
            return new Reference<T> {
                Id = doc.Id,
                Name = doc.Name
            };
        }
}

那么,如果我有一个对象具有..

So then, if I have an object that has ..

List<Reference<Product>>我可以直接分配产品,它只会存储ID/名称.这是我的目标.但是,当涉及到列表比较时,这会很棘手.

List<Reference<Product>> I can assign products directly, and it will only store the Id/Name. This is my goal. However when it comes to list comparison this gets tricky.

那么,假设我有一个像这样的数组.

So then, assume I have an array like this ..

[
 { 
   "Id" : 1, 
   "Requirement" : { 
     "Value" : "2", "Name" : "Orders" 
    }
 },
 { 
   "Id" : 2, 
   "Requirement" : { 
     "Value" : "4", "Name" : "Orders" 
    }
 },
 { 
   "Id" : 3, 
   "Requirement" : { 
     "Value" : "6", "Name" : "Orders" 
    }
 },
 { 
   "Id" : 4, 
   "Requirement" : { 
     "Value" : "8", "Name" : "Orders" 
    }
 },
]

这是Goals.然后假设一个数组List<Reference<Goal>>.这只是实现的目标IdName的列表. (为了弄清楚,这只是一个琐碎的示例.我知道它并不完全是实用的形状)

And this is Goals. Then assume an array, List<Reference<Goal>>. This is nothing but a list of Id and Name of goals attained. (This is just a trivialized example for the sake of figuring this out. I know it is not entirely a practical shape)

然后,正如我在其他问题中所述(在此处可见:

So then, as stated in my other question (visible here : Select All that do not already exist in destination) I wanted to get the "Goals" that were met without getting duplicates each time it was run. This was solved with a simple query..

Orders.Where(o => o.Requirement.Value > requirment).Except(processedOrders);

如果目标数组和源数组相同,但是我的'target'是List<Reference<Order>>,而我的源文件是List<Goal>则可以.数组工作到一个点...

That works if the target array and the source array are the same, but my 'target' is a List<Reference<Order>> and my source is a List<Goal>. The array works up to a point ...

假设User的对象Notebook具有两个数组. List<Reference<Goal>> GoalsReached List<OrdersPlaced>. OrdersPlaced看起来像这样..

Assuming that User has an object Notebook with two arrays. List<Reference<Goal>> is GoalsReached and List<OrdersPlaced>. where OrdersPlaced looks something like this ..

class OrdersPlaced {
  string ProductId { get; set; }
  int Quantity { get; set; }
}

var filtered = goals.Where(n => n.Requirements.Any(r => r.Name == order.Name))
                            .Where(m => m.Requirements.Any(p => p.Total <= order.Total))
                            .Select(n => n.Id)
                            .Except(user.Notebook.GoalsReached);

这是问题开始的地方. GoalsReached只是非规范化的引用列表,如果不进行大量重新组装,就无法将其反向转换为原始对象.有什么方法可以使这种比较更加流畅?

This is where the problem begins. GoalsReached is just a denormalized list of references, and cannot be back-converted to the original object without a lot of re-assembly. Is there any way to make this comparison smoother?

推荐答案

除了使用else之外,它需要使用相同类型的数据进行比较.您应该按照以下步骤进行操作

Instead of using except, which requires having data of the same type to compare. You should do this as follows

var filtered = goals.Where(n => n.Requirements
                                 .Any(r => r.Name == order.Name))
                                 .Where(m => m.Requirements.Any(p => p.Total <= order.Total) 
                                             && !user.Notebook.GoalsReaches.Any(g => g.ID == n.Id))
                                 .Select(n => n.Id);

这篇关于LINQ:选择所有与单个字段匹配的内容,并在原始字段上合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆