Subsonic 3 - 序列不包含匹配元素

发布于 2024-09-03 22:44:51 字数 909 浏览 5 评论 0原文

我需要帮助创建亚音速 LINQ SQL。首先是基础知识,这工作正常:

var query = (from o in bd.concelhos
                     orderby o.descricao
                     select o);

        var results = query.ToList<concelhos>();

但是,我想过滤掉一些列,并且我创建了以下代码:

var query = (from o in bd.concelhos
                     orderby o.descricao
                     select new FilteredConcelhos { id = o.idDistrito + "/" + o.idConcelho, descricao = o.descricao });

        var results = query.ToList<FilteredConcelhos>();

在 ToList 方法中出现错误,并带有描述“序列不包含匹配元素”

任何帮助都会很好。 ..

更新: 原来我在新声明的类中缺少 get set 属性...... 像这样

public class FilteredConcelhos
{
    public string id { get; set; }
    public string descricao { get; set; }
}

这清除了异常,但生成的列表仍然全部错误(FilteredConcelhos.id 不包含任何内容,而 FilteredConcelhos.descricao 包含数字)

I need help creating a LINQ SQL with subsonic. First the basics, this works fine:

var query = (from o in bd.concelhos
                     orderby o.descricao
                     select o);

        var results = query.ToList<concelhos>();

However, I want to filter out some columns and I have created the following code:

var query = (from o in bd.concelhos
                     orderby o.descricao
                     select new FilteredConcelhos { id = o.idDistrito + "/" + o.idConcelho, descricao = o.descricao });

        var results = query.ToList<FilteredConcelhos>();

which errors out in the ToList method with the description "Sequence contains no matching element"

Any help would be great with this...

update:
Turns out I was missing get set attributes in the newly declared class...
Like so

public class FilteredConcelhos
{
    public string id { get; set; }
    public string descricao { get; set; }
}

This clears the exception, but the resulting List is still all wrong (FilteredConcelhos.id contains nothing and FilteredConcelhos.descricao contains numbers)

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

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

发布评论

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

评论(3

和我恋爱吧 2024-09-10 22:44:58

不幸的是,这种情况在我身上发生过很多次。我不确定 Linq 2 Object 如何工作的细节,但如果您对原始对象调用 ToList,如下所示:

from o in bd.concelhos.ToList()
...

它应该可以解决问题。

Unfortunately, this happened to me a lot. I'm not sure about the details of how Linq 2 Object works, but if you'll call ToList on the original object, like this:

from o in bd.concelhos.ToList()
...

It should do the trick.

风轻花落早 2024-09-10 22:44:56

您是否尝试过使用匿名类型?

var query = (from o in bd.concelhos
                 orderby o.descricao
                 select new { id = o.idDistrito + "/" + o.idConcelho, 
                              descricao = o.descricao });

var results = query.ToList();

Have you tried to work with an anonymous type?

var query = (from o in bd.concelhos
                 orderby o.descricao
                 select new { id = o.idDistrito + "/" + o.idConcelho, 
                              descricao = o.descricao });

var results = query.ToList();
⒈起吃苦の倖褔 2024-09-10 22:44:54

您可以尝试先执行 ToList,然后再执行选择 - 然后通过 linq 2 对象执行选择!

Can you try to first execute the ToList and the select afterwards - then the select is performed via linq 2 objects!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文