7

So, here's my situation. I've got a my two classes:

class FromClass
{
    public string[] Foo { get; set; }
}

class ToClass
{
    public string[] Foo { get; set; }
}

The classes have properties which are arrays. They could be List<T> or IEnumerable<T>, I get the same result in any of those cases.

I try to map from one to the other using the AutoMapper.QueryableExtensions:

class Program
{
    static void Main(string[] args)
    {
        // create a "From" object
        string[] anArray = new string[] { "a", "b" };
        FromClass anObject = new FromClass() { Foo = anArray };

        // make a queryable set that includes the "From" object
        IQueryable<FromClass> queryableObjects = (new FromClass[] { anObject }).AsQueryable();

        // set up AutoMapper
        Mapper.CreateMap<FromClass, ToClass>();
        Mapper.AssertConfigurationIsValid();

        // test plain mapping
        IQueryable<ToClass> test1 = queryableObjects.Select(o => Mapper.Map<FromClass, ToClass>(o));
            // success!

        // test queryable extensions
        IQueryable<ToClass> test2 = queryableObjects.Project().To<ToClass>();
            // InvalidOperationException: "Sequence contains no elements"

    }
}

Why does test2 throw an InvalidOperationException? If I make the type of Foo something that's not a collection, e.g. a string or some other class -- then everything works perfectly.

Am I doing something wrong? Not understanding something? Or have I hit a bug?


Objective C: How to grab a number after a sub string from string

I have a lengthy string as below

Please plan to attend to provide upgrade to existing code

morning meeting to acoomdate bum team members

Meeting Number: 457 231 123


To join this meeting

  1. go to http://domainname.com
  2. enter password

Now i want to grab number after the text "Meeting Number" i.e. 457 231 123

Any help please. thanks

EDIT

Lets say i have a string

NSString *myString = @"Please plan to attend to provide upgrade to existing code morning meeting to acoomdate bum team members  Meeting Number: 457 231 123 ----------------------------------------------------- to join this meeting ------------------------------------------------------  1. go to http://domainname.com 2. enter password"
4

1 回答 1

8

我会说:这是一个错误:请参阅Github Issue 159

AutoMapper.QueryableExtensions 在Mapper.CreateMapExpression内部使用,所以如果你写:

var expression = Mapper.CreateMapExpression<FromClass, ToClass>();

它也将失败并出现相同的异常。

目前似乎Mapper.CreateMapExpression不支持:

  • 值类型的通用集合,例如List<string>等。
  • 复杂类型或值类型的数组,例如string[]Bar[]

但是,如果你做到Foo这一点,List<Item>它可以工作:

public class FromClass
{
    public List<Item> Foo { get; set; }
}

public class ToClass
{
    public List<Item> Foo { get; set; }
}

public class Item
{
    public string Bar { get; set; }
}

var list =  new List<Item> { new Item{ Bar = "a"}, new Item() { Bar= "b" }};
FromClass anObject = new FromClass() { Foo = list };
var queryableObjects = (new FromClass[] { anObject }).AsQueryable();
Mapper.CreateMap<FromClass, ToClass>();
Mapper.CreateMap<Item, Item>();
var test2 = queryableObjects.Project().To<ToClass>().ToArray();

您可以评论上述问题或使用您的代码创建一个新问题(这是一个很好的错误重现)

于 2012-04-24T05:50:04.657 回答