views:

200

answers:

2

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)

A: 

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();
SchlaWiener
problem is that I want to return results back. How can I return an anonymous type?
+1  A: 

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

Saintedlama
Can you provide some code? How is it possible to first execute the ToList and then the select?
Try something like this:var query = (from o in bd.concelhos orderby o.descricao select o); var results = query.ToList().Select(o => new FilteredConcelhos { id = o.idDistrito + "/" + o.idConcelho, descricao = o.descricao });
Saintedlama