开发者

generate an array of item consist of 3 different "sub-arrays"

开发者 https://www.devze.com 2023-02-28 19:34 出处:网络
I have a class: class All{ A a; B b; C c; } Now I get 3 arrays: A[] as; B[] bs; C[] cs; Each one of them can be empty (length=0) or null.

I have a class:

class All{
 A a;
 B b;
 C c;
}

Now I get 3 arrays:

A[] as;
B[] bs;
C[] cs;

Each one of them can be empty (length=0) or null.

I need to create a list of Alls objects consists on the开发者_JAVA技巧 arrays where there is at least one element (I don't need the empty object).

For example:
    A[] as={a1, a2};
    B[] bs{};
    C[] cs{c1, c2};
 => Result: All[] = {
      All{a: a1, b:null, c:null},
      All{a: a1, b:null, c:c1},
      All{a: a1, b:null, c:c2},
      All{a: a2, b:null, c:null},
      All{a: a2, b:null, c:c1},
      All{a: a2, b:null, c:c2}
      All{a: null, b:null, c:c1},
      All{a: null, b:null, c:c2}
      //All{a: null, b:null, c:null} -> This is an empty object and I don't need it
};

How can I generate the All[]?


Is this what you are looking for? ( you might have to polish a bit though)

List<A> awithnull = as.ToList();
List<B> bwithnull = bs.ToList();
List<C> cwithnull = cs.ToList();

awithnull.Add(null);
bwithnull.Add(null);
cwithnull.Add(null);

var result = from ae in awithnull
             from be in bwithnull
             from ce in cwithnull
             where (!(ae==null && be ==null && ce == null))
             select new All() {a = ae, b = be, c = ce};


Given this definition of Alls:

class All
{
    public A A { get; set; }
    public B B { get; set; }
    public C C { get; set; }
}

Something like this should work:

A[] myAs = new [] { new A(), new A(), new A()};
B[] myBs = new B[] {};
C[] myCs = new [] {new C(), new C()};

var combinations = (from a in myAs.Concat(new A[] { null })
                    from b in myBs.Concat(new B[] { null })
                    from c in myCs.Concat(new C[] { null })
                    where (!(a == null && b == null && c == null))
                    select new All() { A = a, B = b, C = c }).ToArray();


use object declare:

Object[] All;

You can insert any objects including all the classes you have created.

0

精彩评论

暂无评论...
验证码 换一张
取 消