Subsonic 3 - 序列不包含匹配元素
我需要帮助创建亚音速 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不幸的是,这种情况在我身上发生过很多次。我不确定 Linq 2 Object 如何工作的细节,但如果您对原始对象调用 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:
It should do the trick.
您是否尝试过使用匿名类型?
Have you tried to work with an anonymous type?
您可以尝试先执行 ToList,然后再执行选择 - 然后通过 linq 2 对象执行选择!
Can you try to first execute the ToList and the select afterwards - then the select is performed via linq 2 objects!