Linq and SubSonic - returning nested complex types

烂漫一生 提交于 2019-12-04 15:32:38

You have to call ToList(), otherwise the SubSonic provider tries to do something with MyNestedType and it doesn't exist in the database.

var addresses = from address in repo.All<DatabaseAddress>().ToList()
                        select new Address { MyNestedType = new NestedType { Field1 = address.Address1 } };

Update: It also works if you call ToList afterwards, i.e.:

addresses.ToList().ForEach(address => Console.WriteLine("Address.MyNestedType.Field1 = {0}", address.MyNestedType.Field1));

I guess there is a bug in the SubSonic query provider, because it does work for anonymous types, as you mentioned.

quentin-starin

Please see my question and answer here.

Here's how you can test if it is the same issue:

In that sample code you posted, change Field1 in your NestedType to be named Address1. Re-run your sample. If it works, same issue and the fix I answered with in the linked question should solve it for you.

Try this

var nestedTypes= from address in Database.Addresses.All()
                select new NestedType()
                {
                 Field1 = address.ADDR1
                };
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!