Linq 和 SubSonic - 返回嵌套的复杂类型

发布于 2024-09-10 15:28:20 字数 1912 浏览 4 评论 0原文

我是 SubSonic 的新手,对 LINQ 也相当陌生,所以我只是想将一个小应用程序放在一起。

我已经将模板全部排序并运行正常,但是我在这个 LINQ 语句中遇到了一些麻烦(稍微简化一下,真正的语句有一些其他连接,但它们不会影响这个特定问题,所以我已经为简洁起见删除了它们):

var addresses = from address in Database.Addresses.All()
                           select new Address()
                               {
                                   MyNestedType = new NestedType()
                                       {
                                           Field1 = address.ADDR1
                                       }
                               };

如果我执行此语句,当我尝试枚举结果时,我会收到错误从“System.String”到“NestedType”的转换无效。

我可能忽略了显而易见的事情,但我看不到我请求此类转换的任何地方。

Field1address.ADDR1 都是字符串。

有什么想法我做错了吗?

编辑

我对此进行了另一次研究,为了提供更多信息,我使用 SimpleRepository 和 SQLite 数据库创建了一个小型完整示例来演示该问题。使用 SimpleRepository 我得到的错误是不同的(序列不包含元素),但结果是相同的。完整的代码如下:

 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 返回“Sequence 不包含任何元素”。

如果我在 select 语句中使用匿名类型而不是具体类型,它就可以工作。

显然我的知识有差距;任何帮助表示赞赏。

I'm new to SubSonic and reasonably new to LINQ as well, so I'm just trying to put a little app together.

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
                                       }
                               };

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.

Both Field1 and address.ADDR1 are strings.

Any ideas what I'm doing wrong?

Edit:

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 } };
    }
 }

In this example, all contains the object added to the database, but addresses returns "Sequence contains no elements".

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

抠脚大汉 2024-09-17 15:28:20

此处查看我的问题和答案。

以下是测试是否是同一问题的方法:

在您发布的示例代码中,将 NestedType 中的 Field1 更改为 Address1。重新运行您的示例。如果它有效,同样的问题和我在链接问题中回答的修复应该可以为您解决。

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.

感受沵的脚步 2024-09-17 15:28:20

您必须调用 ToList(),否则 SubSonic 提供程序会尝试对 MyNestedType 执行某些操作,但它在数据库中不存在。

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

更新:如果您之后调用 ToList,它也可以工作,即:

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

我猜 SubSonic 查询提供程序中有一个错误,因为正如您提到的,它确实适用于匿名类型。

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.

心奴独伤 2024-09-17 15:28:20

试试这个

var nestedTypes= from address in Database.Addresses.All()
                select new NestedType()
                {
                 Field1 = address.ADDR1
                };

Try this

var nestedTypes= from address in Database.Addresses.All()
                select new NestedType()
                {
                 Field1 = address.ADDR1
                };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文