English 中文(简体)
C#: Projection - Access of a givenstring property in a Collection of Object in same way as a Liststring
原标题:C#: projection - accessing a given string property in a collection of objects in the same way as a List<string>

a 快速预测问题。 我用测试方法收集物体(交换项目),并谨确保这些物体与我的预期内容相符。 如你所显示的那样,这只是一个理想。 我确信,它有可能利用预测来缩小规模(LMAA_CODE-不问首都!) 遵循以下底线。 页: 1

感谢!

// Works but not very elegant
            Assert.AreEqual(result.unchangedItems[0].LMAA_CODE,expectedunchangedItems[0]);
            Assert.AreEqual(result.unchangedItems[1].LMAA_CODE,expectedunchangedItems[1]);
            Assert.AreEqual(result.unchangedItems[2].LMAA_CODE,expectedunchangedItems[2]);
            Assert.AreEqual(result.unchangedItems[3].LMAA_CODE,expectedunchangedItems[3]);

        // ?Can something like this be done?  eg result.unchangedItems => result.unchangedItems.LMAA_CODE
        Assert.IsTrue(Enumerable.SequenceEqual(result.unchangedItems.LMAA_CODE, expectedunchangedItems));
最佳回答

You were almost there already, just added a projection to LMAA_CODE:

 Assert.IsTrue(Enumerable.SequenceEqual(result.unchangedItems.Select( x=> x.LMAA_CODE), 
                                        expectedunchangedItems));
问题回答

您可使用LINQ和Zip:

Assert.IsTrue(result.unchangedItems.Zip(expectedunchangedItems, (actual, expected) => actual.LMAA_CODE == expected).All(value => value));

什么是:

for (int i = 0; i < result.unchangedItems.Count; i++) {
    Assert.AreEqual(result.unchangedItems[i].LMAA_CODE, expectedUnchangedItems[i]);
}

也可以做:

Assert.IsFalse(result.unchangedItems.Where((r,i) => expectedUnchangedItems[i] != r.LMAA_CODE).Any())

.Any() will return true if the filtered sequence contains elements, and it shouldn t. That s why you call IsFalse. If your testing framework (or whatever you are using) does not have that, you can always do Assert.AreEqual(____, false).

You could also use a "Zip" extension method.

Assert.AreEqual(result.unchangedItems.Count, expectedunchangedItems.Count);
result.unchangedItems.Zip(expectedunchangedItems, (a, b) => Tuple.Create(a, b))
                     .ForEach(p => Assert.AreEqual(p.Item1.LMAA_CODE, p.Item2));

这样,例外报告就没有价值。 Zip()方法包含在“NET”4中,但我个人宁愿使用我自己的方法,当然,你也希望“ForEach(......)”:

// I actually have a Pair<A,B> struct that I prefer to use, but 
// KeyValuePair or Tuple works almost as well.
public static IEnumerable<Tuple<A, B>> ZipTuples<A, B>(this IEnumerable<A> a, IEnumerable<B> b)
{
    IEnumerator<A> ea = a.GetEnumerator();
    IEnumerator<B> eb = b.GetEnumerator();
    while (ea.MoveNext() && eb.MoveNext())
        yield return new Tuple<A, B>(ea.Current, eb.Current);
}
public static void ForEach<T>(this IEnumerable<T> list, Action<T> action)
{
    foreach (T item in list)
        action(item);
}

Usage:

Assert.AreEqual(result.unchangedItems.Count, expectedunchangedItems.Count);
result.unchangedItems.ZipTuples(expectedunchangedItems)
                     .ForEach(p => Assert.AreEqual(p.Item1.LMAA_CODE, p.Item2));




相关问题
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. ...

热门标签