用于数据聚合的 Linq 查询
我有这个类,
public class Line
{
public string ConnectionsIndex{get;set;}
}
我的 Linq 问题是我必须将这些行聚合
var l1 = new Line{ ConnectionsIndex="01,02"};
var l2 = new Line{ ConnectionsIndex="02,03"};
var l3 = new Line{ ConnectionsIndex="01,03"};
到这个类中,
var l4 = new Line{ ConnectionsIndex="01,02,03"};
这可以用 Linq 来做吗?
细节:
当我添加我收藏中的其他物品时,事情变得更加复杂(至少对我来说)。
var l5 = new Line (ConnectionsIndex = "02,04");
var l6 = new Line (ConnectionsIndex = "03,06");
因为不存在其他线对 03,04 、 01,04 、 01,06 和 02,06
我不知道我是否解释得很好......
在实践中,想象你有一个多边形的所有点,我想通过给出每个多边形所有点之间的连接列表来从查询中获取所有项目的一行。
(我的列表包含多个多边形)
如果没有连接到所有其他点,则不应将一个点包含在结果中。
这是我的列表内容的示例:
ConnectionsIndex="166,171"
ConnectionsIndex="166,174"
ConnectionsIndex="166,333"
ConnectionsIndex="169,170"
ConnectionsIndex="171,175"
ConnectionsIndex="171,334"
ConnectionsIndex="167,174"
ConnectionsIndex="172,174"
ConnectionsIndex="174,335"
ConnectionsIndex="177,341"
ConnectionsIndex="180,200"
ConnectionsIndex="181,183"
ConnectionsIndex="182,199"
ConnectionsIndex="184,185"
ConnectionsIndex="186,188"
ConnectionsIndex="189,192"
ConnectionsIndex="190,230"
ConnectionsIndex="191,375"
在此列表中,您有例如 166、171 和 334 之间的三角形
更多详细信息:
var group =lines.Where(x => x.ConnectionsIndex.Split(',').Contains (行.ConnectionsIndex.Split(',')[0]) || x。 ConnectionsIndex.Split(',').Contains(line.ConnectionsIndex.Split(',')[1])).ToList(); if (group.Count()==1) { Straight_lines.Add(线); } 别的 { //这里我有一个“组”,其中包含点之间的所有线..我想获得不同的点 }
I have this class
public class Line
{
public string ConnectionsIndex{get;set;}
}
my Linq problem is that I have to aggregate these Lines
var l1 = new Line{ ConnectionsIndex="01,02"};
var l2 = new Line{ ConnectionsIndex="02,03"};
var l3 = new Line{ ConnectionsIndex="01,03"};
into this
var l4 = new Line{ ConnectionsIndex="01,02,03"};
It's possible to do with Linq?
DETAIL:
The thing is more complicate (at least for me) when I add the other items that I have in my collection.
var l5 = new Line (ConnectionsIndex = "02,04");
var l6 = new Line (ConnectionsIndex = "03,06");
because do not exist other lines with the pairs 03,04 , 01,04 , 01,06 and 02,06
I do not know if I have explained it well ...
in practice, imagine you have all the points of a polygon, I want to get a line of all the items from the query by giving a list of connections between all points of each polygon.
(my list contains more than one polygon)
One point should not be included in result if not connected to all others.
This is an example of my list content:
ConnectionsIndex="166,171"
ConnectionsIndex="166,174"
ConnectionsIndex="166,333"
ConnectionsIndex="169,170"
ConnectionsIndex="171,175"
ConnectionsIndex="171,334"
ConnectionsIndex="167,174"
ConnectionsIndex="172,174"
ConnectionsIndex="174,335"
ConnectionsIndex="177,341"
ConnectionsIndex="180,200"
ConnectionsIndex="181,183"
ConnectionsIndex="182,199"
ConnectionsIndex="184,185"
ConnectionsIndex="186,188"
ConnectionsIndex="189,192"
ConnectionsIndex="190,230"
ConnectionsIndex="191,375"
In this List you have for example a triangle between 166, 171 and 334
More detail:
var group = lines.Where(x => x.ConnectionsIndex.Split(',').Contains(line. ConnectionsIndex.Split(',')[0]) ||
x. ConnectionsIndex.Split(',').Contains(line. ConnectionsIndex.Split(',')[1])).ToList();
if (group.Count()==1)
{
straight_lines.Add(line);
}
else
{
//Here I have a "group" with all the lines between point.. I want to get distinc points
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
类似于:
这不会对连接进行排序,但如果需要,您可以轻松添加它。
当然,如果您愿意将
ConnectionsIndex
作为字符串集合而不是单个分隔字符串,那么这一切都会更清晰:)Something like:
This doesn't order the connections, but you can easily add that if you need it.
This would all be cleaner if you were happy to have
ConnectionsIndex
as a collection of strings instead of a single delimited string, of course :)我用过这个:
I used this:
这是我发现的坏方法......但它有效!
如果您发现或知道更好的解决方案,欢迎您!
This is the bad way I have found... and it works!
If you find or known better solutions, you are welcome!
请注意,我认为您要求的是一种找到最大派系(图论中的概念)的方法。众所周知,这是一个 NP 难问题。我认为您的版本有时会起作用,并且希望适用于您感兴趣的那些情况。但是,不适用于任何事物可能与其他事物相关的复杂情况。事实上,如果您有很多节点,那么即使有大量 CPU 周期预算(无论 LINQ 如何),这些情况也是不可行的。
Just a note that I think what you're asking for is a way to find maximal cliques (a concept from graph theory). This is known to be an NP-Hard problem. I think your version will work sometimes, and hopeflly for those cases you're interested in. But, not for complicated cases where anything may be connected to anything else. Indeed, if you have a lot of nodes, those cases aren't feasible, even with large CPU cycle budgets (regardless of LINQ).