English 中文(简体)
Linq 交叉盘点密封名单
原标题:Linq cross join query for nested List

在我 st的情景中,请帮助我。

就像这一点一样。

  1. 利用不动产业分类编制了一份动态编制的表和外地清单(见表)。

    BindingList<Table> table = new BindingList<Table>();        
    [Serializable]
    [TypeConverter(typeof(TableConverter))]
    public class Table {
    private string _name = string.Empty;
    private HeaderCollection _hc = new HeaderCollection();
    private BindingList<Fields> _fc = new BindingList<Fields>();
    public Guid key;
    public Table() {
        key = Guid.NewGuid();
    }
    
    
    [DisplayName( "Table Fields" ), Editor( typeof( FieldCollectionEditor ),
    typeof( UITypeEditor ) )]
    public BindingList<Fields> Fields {
        get { return _fc; }
        set { _fc = value; }
    }
    [DisplayName( "Table Header" )]
    public HeaderCollection Headers {
        get { return _hc; }
        set { _hc = value; }
    }
    
    
    [DisplayName( "Table Name" )]
    public string Name {
        get { return _name; }
        set { _name = value; }
        }
    }
    
  2. 外地类别定义

    [Serializable]
    public class Fields {
    private string _name = string.Empty;
    public Guid Key;
    private List<string> _value = new List<string>();
    
    
    [Browsable( false )]
    public List<string> Value {
        get { return _value; }
        set { _value = value; }
    }       
    
    
    public Fields() {
        Key = Guid.NewGuid();
    }
    
    
    [DisplayName( "Field Name" )]
    public string Name {
        get { return _name; }
        set { _name = value; }
    }
    
    
    [DisplayName( "Map" )]
    public bool Map {
        get;
        set;
        }
    }
    
  3. 外地班级 显示价值一项或多项的清单。

My Problem is : Need to cross join all values beloging to all fields from a table and display the data in tabular format. I have used this query, but this does not work as it fetch out the values one by one, instead i need a coross join of all values from all fields at one go.

var result = table.SelectMany(
    tbl => tbl.Fields.SelectMany(
      f => f.Value.Select(v => new { i = v })));

For Example, Lets say :

F1 has Value11
F2 has Value21
F3 has Value31 and Value 32
F4 has Value41, Value42 and Value43

其结果应采用表格的这种格式和所有领域的价值。

Value11 Value21 Value 31 Value 41
Value11 Value21 Value 31 Value 42
Value11 Value21 Value 31 Value 43
Value11 Value21 Value 32 Value 41
Value11 Value21 Value 32 Value 42
Value11 Value21 Value 32 Value 43

让我进一步阐述这一点。 例如,我们

List<string> master = new List<string>(); 
List<string> child = new List<string>(); 
List<string> child1 = new List<string>(); 
List<string> child2 = new List<string>(); 
and a Linq query to fetch out

var q = from m in master 
        from c1 in child1 
        from c in child 
        from c2 in child2 
        select new { m, c, c1, c2 };

我确实需要写上上述问题,以勾销外地的价值观,但问题是动态产生的领域,因此,内部的价值观需要某种改观方法或气温克程序,才能取得上述抽样结果。

问题回答

你似乎有一个结构,删除了所有其他细节:

  • Table has a collection of fields
  • Field has a collection of values

因此,所有价值观都贯穿各个领域。 我首先要收集所有领域的所有价值观(利用两个问题获得两种机会):

var values = from v in (from f in Table.Fields select f.Values)
             select v;

然后加入:

var res = from v in values
          from f in Table.Fields
          select new {
             Field = f,
             Value = v
          };

这应涵盖在第一个职位上的主人/儿童样本:

class Program
{
    static void Main(string[] args)
    {
        var listofInts = new List<List<int>>(3);
        listofInts.Add(new List<int>{1, 2, 3});
        listofInts.Add(new List<int> { 4,5,6 });
        listofInts.Add(new List<int> { 7,8,9,10 });

        var temp = CrossJoinLists(listofInts);
        foreach (var l in temp)
        {
            foreach (var i in l)
                Console.Write(i + ",");
            Console.WriteLine();
        }
    }

    private static IEnumerable<List<T>> CrossJoinLists<T>(IEnumerable<List<T>> listofObjects)
    {
        var result = from obj in listofObjects.First()
                     select new List<T> {obj};

        for (var i = 1; i < listofObjects.Count(); i++)
        {
            var iLocal = i;
            result = from obj  in result
                     from obj2 in listofObjects.ElementAt(iLocal)
                     select new List<T>(obj){ obj2 };
        }

        return result;
    }
}

增 编 for you immediate response I tried your approach but still not gettting values in tabluar format. Let me clear the details once again.

说 明

Field1 has Value11
Field2 has Value21
Field3 has Value31 and Value32
Field4 has Value41, Value42 and Value43

所有这些领域都属于单一表格。

现在,在十字军加入之后,结果就好像如下。

Value11 Value21 Value31 Value41
Value11 Value21 Value31 Value42
Value11 Value21 Value31 Value43
Value11 Value21 Value32 Value41 
Value11 Value21 Value32 Value42
Value11 Value21 Value32 Value43

.......
------

etc.  

增 编

Buzzy





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签