首页 \ 问答 \ 由成员交叉的两个列表(Intersection of two lists by a member)

由成员交叉的两个列表(Intersection of two lists by a member)

我有一个像这样的对象:

public class myObject
{
public string Name {get; set;}
public string ID {get; set;}
}

我有两个这样的列表

List<myObject> list1 = new List<myObject>();
list1.Add(new myObject(){Name = Jason, ID = 1});
list1.Add(new myObject(){Name = Jonathan, ID = 2});
list1.Add(new myObject(){Name = Kevin, ID = 3});

List<myObject> list2 = new List<myObject>();
list2.Add(new myObject(){Name = Jennifer, ID = 5});
list2.Add(new myObject(){Name = Samantha, ID = 2});
list2.Add(new myObject(){Name = Lucy, ID = 9});

我希望通过ID来交叉这两个列表。 我的意思是我想把Jonathan和Samantha的对象放在另一个列表中。 我怎样才能做到这一点? 谢谢。


I have an object like this :

public class myObject
{
public string Name {get; set;}
public string ID {get; set;}
}

I have two lists like this

List<myObject> list1 = new List<myObject>();
list1.Add(new myObject(){Name = Jason, ID = 1});
list1.Add(new myObject(){Name = Jonathan, ID = 2});
list1.Add(new myObject(){Name = Kevin, ID = 3});

List<myObject> list2 = new List<myObject>();
list2.Add(new myObject(){Name = Jennifer, ID = 5});
list2.Add(new myObject(){Name = Samantha, ID = 2});
list2.Add(new myObject(){Name = Lucy, ID = 9});

I want to intersect these two lists by their IDs. I mean I want to get Jonathan's and Samantha's objects in another list. How can I do that? Thanks.


原文:https://stackoverflow.com/questions/44022114
更新时间:2024-03-29 15:03

最满意答案

我认为这样的事情应该有效:

branch :: Tree a b -> [[b]]
branch (Leaf _) = [[]]
branch (Branch (a, right) (b, left)) = map (a :) (branch right) 
                                    ++ map (b :) (branch left)

I think something like this should work:

branch :: Tree a b -> [[b]]
branch (Leaf _) = [[]]
branch (Branch (a, right) (b, left)) = map (a :) (branch right) 
                                    ++ map (b :) (branch left)

相关问答

更多
  • 这是一个单行解决方案,虽然不太高效: parents = [ 2, 3, 1, 5, 4, 7, 8, 9, 6, 10 ] children = [ [4,5], [0,11], [6,10], [1,7], [8,9], [12,13], [14,15], [16,17], [18,19], [20,21] ...
  • 我认为这样的事情应该有效: branch :: Tree a b -> [[b]] branch (Leaf _) = [[]] branch (Branch (a, right) (b, left)) = map (a :) (branch right) ++ map (b :) (branch left) I think something like this should work: branch :: Tree a b -> ...
  • 您可以使用递归函数从顶部解析treeview ,因此treeview每个根音都是一个SQL语句: 例如: 功能码: string getHead(TreeViewItem t)             {                 string s = "";                 if (t.Items.Count == 0) //get the condition                 {                     return s=t.Header.ToStri ...
  • 经过长时间的搜索和尝试错误的方法,我已经设法找到一个关闭自定义级别的孩子的解决方案 这是lvl 3孩子的解决方案 $('.tree li>ul>li>ul').hide(); $('.tree li:first').show(); After a long search and try-error methods , i've manage to find a solution for closing at a custom level of children here is the solution f ...
  • 这是非常不寻常的(例如,问一个树与其所有节点的平面列表之间的关系更为常见,DCG很适合),可能是这样的: tree_list(nil, []). tree_list(tree(Node,Left,Right), [Lefts,Node,Rights]) :- tree_list(Left, Lefts), tree_list(Right, Rights). 例: ?- tree_list(tree(a,tree(b,nil,tree(d,nil,nil)),tree(c,n ...
  • 很酷的问题,工作很有趣。 我试图彻底,结果变得很长,我希望它仍然可读。 码: ########################## #Input data cleaned a bit# ########################## lines = ["Fred,Karl,Technician,2010", "Karl,Cathy,VP,2009", "Cathy,NULL,CEO,2007", "Vince,Cathy,Technician,20 ...
  • 您可以在构建定义的末尾添加powershell脚本任务以通过Rest API触发发布,我创建了一个简单的代码示例供您参考: if ($env:BUILD_SOURCEBRANCHNAME -eq "master") { $collectionuri = $env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI $buildid = $env:BUILD_BUILDID $project = $env:SYSTEM_TEAMPROJECT $token = $env:SYSTEM_ACC ...
  • 1) 首先,我注意到你正在做map fa尽管a是单个值,而不是列表¹。 所以你应该做的不是map fa 。 现在问你实际问到的问题: 你是对的,它不起作用,因为c是一个树列表, mapmtree只需要一棵树。 所以你会怎么做? 您将mapmtree应用于树列表中的每个树,然后使用生成树的列表作为新Branch的树列表。 你是怎样做的? 使用列表中的map : mapmtree f (Branch a c) = Branch (f a) (map (mapmtree f) c) 2) 和1)一样,你使用ma ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)