Select in Linq. Could someone, please, check it out?

S

shapper

Hello,

I have two tables: Polls and Options:

Poll > PollID, Question
Options > OptionID, PollID, Answer

I want to select a Poll given its ID and all Options associated to
it.
Options should be converted to a CSV list ...

My Linq code is working but I feel I am having some redundancy in
it ... Could someone, please, check it out?

PollPaper paper = (from p in database.Polls
where p.PollID == id
select new PollPaper {
Poll = p,
Options = string.Join(", ", (from o in
database.Options
join ps in
database.Polls on o.PollID equals ps.PollID
orderby
o.Answer
select
o.Answer).ToArray())
}).SingleOrDefault();

Thank You,
Miguel
 
J

Jon Skeet [C# MVP]

shapper said:
I have two tables: Polls and Options:

Poll > PollID, Question
Options > OptionID, PollID, Answer

I want to select a Poll given its ID and all Options associated to
it.
Options should be converted to a CSV list ...

Rather than doing the conversion to CSV internally, I'd make the Poll
entity have a collection of Options - which normally the designer will
include for you anyway, assuming you've got an appropriate relationship
specified in the database.

When it's fetched the collection of Polls, each of which has a
collection of Options, you cna do the CSV conversion in memory.
 
S

shapper

Rather than doing the conversion to CSV internally, I'd make the Poll
entity have a collection of Options - which normally the designer will
include for you anyway, assuming you've got an appropriate relationship
specified in the database.

When it's fetched the collection of Polls, each of which has a
collection of Options, you cna do the CSV conversion in memory.

--
Jon Skeet - <[email protected]>
Web site:http://www.pobox.com/~skeet 
Blog:http://www.msmvps.com/jon.skeet
C# in Depth:http://csharpindepth.com

Yes, that was my first option ... however PollPaper is a wrapper that
I created around LinqToSQL class Poll.

PollPaper includes a Poll object and a string of Options.

Why? Because I am using ASP.NET MVC and I am delivering the data from
the Controller to the View.

I could use an object for options ... then on the View convert it to
CSV but then I would need to convert back to an object on the View on
form submission ...
.... because the controller would expect a list of Options.

Remember than in this case PollPaper is an wrapper that works as
ViewData of the View/Controller.

Does this make any sense?

My initial concern was more in relation to my join in the linq.

Thanks,
Miguel
 
J

Jon Skeet [C# MVP]

shapper said:
Yes, that was my first option ... however PollPaper is a wrapper that
I created around LinqToSQL class Poll.

You can still do that, but it'll be easier to do the string conversion
having fetched all the polls + options in a single SQL query.

My initial concern was more in relation to my join in the linq.

Exactly - if you get LINQ to fetch the Options automatically by
relating them to the polls (possibly setting extra fetch criteria -
it's been a while since I've done LINQ to SQL).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Random 6
Please, need help to finish a query. Thank You. 3
Linq - Count 1
Linq. 3 Joins or Lambda expressions 1
Linq. Select. 1
Linq. Please, need help. 1

Top