Linq和SubSonic-返回嵌套的复杂类型 [英] Linq and SubSonic - returning nested complex types
问题描述
我是SubSonic的新手,也是LINQ的新手,所以我只是想将一个小应用程序组合在一起.
I'm new to SubSonic and reasonably new to LINQ as well, so I'm just trying to put a little app together.
我已经对模板进行了排序并可以正常运行,但是我在使用LINQ语句时遇到了一些麻烦(稍微简化一下,真实的语句还有其他一些联接,但是它们不会影响这个特定的问题,所以为简洁起见,我已将其删除):
I've got the templates all sorted and running okay, but I've run into a bit of trouble with this LINQ statement (simplified slightly, the real statement has some other joins but they don't affect this particular problem so I've removed them for brevity):
var addresses = from address in Database.Addresses.All()
select new Address()
{
MyNestedType = new NestedType()
{
Field1 = address.ADDR1
}
};
如果我执行此语句,当我尝试枚举结果时,会出现错误从'System.String'到'NestedType'的无效转换..
If I execute this statement I get the error Invalid cast from 'System.String' to 'NestedType'. when I try to enumerate the results.
我可能忽略了明显的地方,但是我看不到任何要求进行这种转换的地方.
I'm probably overlooking the obvious but I can't see anywhere that I request such a conversion.
Field1 和 address.ADDR1 都是字符串.
有什么想法我在做什么错吗?
Any ideas what I'm doing wrong?
修改:
我对此进行了另一番研究,并且为了提供更多信息,我使用SimpleRepository和一个演示该问题的SQLite数据库创建了一个小的完整示例.使用SimpleRepository,我得到的错误是不同的(序列不包含任何元素),但是结果是相同的.这是完整的代码:
I've had another look at this and in an effort to provide more information, I've created a small, complete example using SimpleRepository and an SQLite database that demonstrates the issue. Using SimpleRepository the error I get is different (Sequence contains no elements) but the result is the same. Here's the complete code:
public class DatabaseAddress
{
public int Id { get; set; }
public string Address1 { get; set; }
}
public class Address
{
public NestedType MyNestedType;
}
public class NestedType
{
public string Field1 { get; set; }
}
static class Program
{
[STAThread]
static void Main()
{
var repo = new SimpleRepository("Db", SimpleRepositoryOptions.RunMigrations);
DatabaseAddress address1 = new DatabaseAddress();
address1.Address1 = "Test";
repo.Add(address1);
var all = repo.All<DatabaseAddress>();
var addresses = from address in repo.All<DatabaseAddress>()
select new Address { MyNestedType = new NestedType { Field1 = address.Address1 } };
}
}
在此示例中,all
包含添加到数据库中的对象,但是addresses
返回序列不包含任何元素".
In this example, all
contains the object added to the database, but addresses
returns "Sequence contains no elements".
如果我在select
语句中使用匿名类型而不是具体类型,那么它将起作用.
If I use anonymous types instead of concrete types in the select
statement it works.
我在这里的知识上显然有差距;任何帮助表示赞赏.
There's obviously a gap in my knowledge here; any help appreciated.
推荐答案
您必须调用ToList(),否则SubSonic提供程序将尝试使用MyNestedType进行某些操作,并且它在数据库中不存在.
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 } };
更新:如果您以后再调用ToList,即:
Update: It also works if you call ToList afterwards, i.e.:
addresses.ToList().ForEach(address => Console.WriteLine("Address.MyNestedType.Field1 = {0}", address.MyNestedType.Field1));
我猜SubSonic查询提供程序中有一个错误,因为它确实适用于匿名类型,正如您提到的那样.
I guess there is a bug in the SubSonic query provider, because it does work for anonymous types, as you mentioned.
这篇关于Linq和SubSonic-返回嵌套的复杂类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!