用于数据聚合的 Linq 查询

发布于 2024-08-04 07:25:33 字数 1837 浏览 4 评论 0原文

我有这个类,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不打扰别人 2024-08-11 07:25:33

类似于:

var connections = (from line in lines
                   from connection in line.Split(',')
                   select connection).Distinct()
                                     .ToArray();

Line line = new Line { ConnectionsIndex = string.Join(",", connections) };

这不会对连接进行排序,但如果需要,您可以轻松添加它。

当然,如果您愿意将 ConnectionsIndex 作为字符串集合而不是单个分隔字符串,那么这一切都会更清晰:)

Something like:

var connections = (from line in lines
                   from connection in line.Split(',')
                   select connection).Distinct()
                                     .ToArray();

Line line = new Line { ConnectionsIndex = string.Join(",", connections) };

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 :)

疏忽 2024-08-11 07:25:33

我用过这个:

var l4 = new Line{ 
    ConnectionsIndex = 
        string.Join(",", (lines.SelectMany(x => x.ConnectionsIndex.Split(','))
                               .Distinct()
                               .OrderBy(s => s)).ToArray())
};

I used this:

var l4 = new Line{ 
    ConnectionsIndex = 
        string.Join(",", (lines.SelectMany(x => x.ConnectionsIndex.Split(','))
                               .Distinct()
                               .OrderBy(s => s)).ToArray())
};
荆棘i 2024-08-11 07:25:33

这是我发现的坏方法......但它有效!

var l = linee.Distinct(
            (a, b) => a.ConnectionsIndex == b.ConnectionsIndex,x=>x.ConnectionsIndex.GetHashCode())
        .ToList();

    var single_lines = new List<Linea>();
    var multiple_lines = new List<Linea>();
    foreach (var linea in l)
    {
        var group = l
            .Where(x => x.ConnectionsIndex.Split(',').Contains(linea.ConnectionsIndex.Split(',')[0]) ||
                x.ConnectionsIndex.Split(',').Contains(linea.ConnectionsIndex.Split(',')[1])).ToList();
        if (group.Count()==1)
        {
            single_lines.Add(linea);
        }
        else
        {
            var indexes = new List<string>();
            var dist = group.Select(x => new {Index = x.ConnectionsIndex.Split(',').ToList()}).ToList();    

            foreach (var linea1 in dist)
            {
        indexes=indexes.Concat(linea1.Index).ToList();
            }

            var indexstring = new StringBuilder();
            foreach (var s in indexes.Distinct().OrderBy(x=>Convert.ToInt32(x)))
            {
        indexstring.Append(s).Append(',');
            }
            indexstring.Remove(indexstring.Length - 1, 1);
            multiple_lines.Add(new Linea() {ConnectionsIndex = indexstring.ToString()});
        }
    }

var multi_distinct=multiple_lines.Distinct(
            (a, b) => a.ConnectionsIndex == b.ConnectionsIndex, x => x.ConnectionsIndex.GetHashCode())
            .ToList();

linee = single_lines.Concat(multi_distinct).ToList();

如果您发现或知道更好的解决方案,欢迎您!

This is the bad way I have found... and it works!

var l = linee.Distinct(
            (a, b) => a.ConnectionsIndex == b.ConnectionsIndex,x=>x.ConnectionsIndex.GetHashCode())
        .ToList();

    var single_lines = new List<Linea>();
    var multiple_lines = new List<Linea>();
    foreach (var linea in l)
    {
        var group = l
            .Where(x => x.ConnectionsIndex.Split(',').Contains(linea.ConnectionsIndex.Split(',')[0]) ||
                x.ConnectionsIndex.Split(',').Contains(linea.ConnectionsIndex.Split(',')[1])).ToList();
        if (group.Count()==1)
        {
            single_lines.Add(linea);
        }
        else
        {
            var indexes = new List<string>();
            var dist = group.Select(x => new {Index = x.ConnectionsIndex.Split(',').ToList()}).ToList();    

            foreach (var linea1 in dist)
            {
        indexes=indexes.Concat(linea1.Index).ToList();
            }

            var indexstring = new StringBuilder();
            foreach (var s in indexes.Distinct().OrderBy(x=>Convert.ToInt32(x)))
            {
        indexstring.Append(s).Append(',');
            }
            indexstring.Remove(indexstring.Length - 1, 1);
            multiple_lines.Add(new Linea() {ConnectionsIndex = indexstring.ToString()});
        }
    }

var multi_distinct=multiple_lines.Distinct(
            (a, b) => a.ConnectionsIndex == b.ConnectionsIndex, x => x.ConnectionsIndex.GetHashCode())
            .ToList();

linee = single_lines.Concat(multi_distinct).ToList();

If you find or known better solutions, you are welcome!

燕归巢 2024-08-11 07:25:33

请注意,我认为您要求的是一种找到最大派系(图论中的概念)的方法。众所周知,这是一个 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文