使用LINQ to压扁一个分层数据集 - 一个警告 [英] Using LINQ to flatten a hierarchical dataset - with a caveat

查看:79
本文介绍了使用LINQ to压扁一个分层数据集 - 一个警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的数据集,我需要压扁成一个列表:

 <排ITEMID =828518871locationID = 60004165TYPEID =9331量=6标志=4单=0/> 
<排ITEMID =830476364locationID =60004165的typeid =649量=1标志=4单身=1>
<行集名=内容键=ITEMID列=ITEMID,TYPEID,数量,标志,单>
<排ITEMID =1139985051的typeid =6485量=1标志=22单身=1/>
<排ITEMID =1773489170的typeid =11489量=1标志=5单身=1>
<行集名=内容键=ITEMID列=ITEMID,TYPEID,数量,标志,单>
<排ITEMID =1001694072954的typeid =16357量=1标志=0单身=0/>
<排ITEMID =1001694110421的typeid =16371量=1标志=0单身=0/>

< /行集>
< /行>
< /行集>
< /行>



... 表示附加行嵌套/或水平;各级理论上可鸟巢无限)



输出需要是这样的:



<。 pre> 项{ID = 828518871,位置= 60004165,类型= 9331,数量= 6,标志= 4,辛格尔顿= FALSE}
项目{ID = 830476364,位置= 60004165 ,类型= 649,数量= 1,标志= 4,辛格尔顿=真}
项目{ID = 1139985051,位置= 60004165,类型= 6485,数量= 1,标记= 22,辛格尔顿=真}
项目{ID = 1773489170,位置= 60004165,类型= 11489,数量= 1,标记= 5,辛格尔顿=真}
项目{ID = 1001694072954,位置= 60004165,类型= 16357,数量= 1,旗= 0,辛格尔顿= FALSE}
项目{ID = 1001694110421,位置= 60004165,类型= 16371,数量= 1,标志= 0,辛格尔顿= FALSE}
...

美中不足的是,输出中的所有行需要有自己的 LocationID 正确设置 - 子行必须从他们的父母,或他们的父母的父母,等这个属性。



我写了一个递归的方法正确生成所需的输出,但我很想得到只使用LINQ相同的结果 - 这是可能的。


解决方案

LINQ ISN? T一般用任意递归不错。它的罚款压扁的有一个的使用的SelectMany 嵌套层,但递归的解决方案将最终仍需要显式调用自己。例如,您可以编写使用LINQ和查询中调用自己的方法,但你不能轻易做出LINQ隐式执行递归。



所以,你可能可以调整现有的递归方法使用LINQ,但你不可能用完全不同的结构来解决告终。


I have the following dataset I need to flatten into a list:

<row itemID="828518871" locationID="60004165" typeID="9331" quantity="6" flag="4" singleton="0"/>
<row itemID="830476364" locationID="60004165" typeID="649" quantity="1" flag="4" singleton="1">
  <rowset name="contents" key="itemID" columns="itemID,typeID,quantity,flag,singleton">
    <row itemID="1139985051" typeID="6485" quantity="1" flag="22" singleton="1"/>
    <row itemID="1773489170" typeID="11489" quantity="1" flag="5" singleton="1">
      <rowset name="contents" key="itemID" columns="itemID,typeID,quantity,flag,singleton">
        <row itemID="1001694072954" typeID="16357" quantity="1" flag="0" singleton="0"/>
        <row itemID="1001694110421" typeID="16371" quantity="1" flag="0" singleton="0"/>
        ...
      </rowset>
    </row>
  </rowset>
  </row>

(... indicates additional rows and/or levels of nesting; the levels can theoretically nest infinitely.)

The output needs to look like this:

Item { ID = 828518871, Location = 60004165, Type = 9331, Quantity = 6, Flag = 4, Singleton = false }
Item { ID = 830476364, Location = 60004165, Type = 649, Quantity = 1, Flag = 4, Singleton = true }
Item { ID = 1139985051, Location = 60004165, Type = 6485, Quantity = 1, Flag = 22, Singleton = true }
Item { ID = 1773489170, Location = 60004165, Type = 11489, Quantity = 1, Flag = 5, Singleton = true }
Item { ID = 1001694072954, Location = 60004165, Type = 16357, Quantity = 1, Flag = 0, Singleton = false }
Item { ID = 1001694110421, Location = 60004165, Type = 16371, Quantity = 1, Flag = 0, Singleton = false }
...

The catch is that all rows in the output need to have their LocationID correctly set - child rows must get this property from their parent, or their parent's parent, etc.

I've written a recursive method that correctly generates the required output, but I would love to get the same results using only LINQ - is this possible?

解决方案

LINQ isn't generally good with arbitrary recursion. It's fine for flattening one layer of nesting using SelectMany, but recursive solutions will still end up needing to call themselves explicitly. For example, you can write a method which uses LINQ and which calls itself within the query, but you can't easily make LINQ perform the recursion implicitly.

So you may be able to tweak your existing recursive method to use LINQ, but you're unlikely to end up with a completely different structure to the solution.

这篇关于使用LINQ to压扁一个分层数据集 - 一个警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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