By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,953 Members | 1,131 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,953 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Sep 5 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
shapper <md*****@gmail.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Sep 6 '08 #2

P: n/a
On Sep 6, 7:30*am, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
shapper <mdmo...@gmail.comwrote:
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.

--
Jon Skeet - <sk...@pobox.com>
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
Sep 6 '08 #3

P: n/a
shapper <md*****@gmail.comwrote:
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.

<snip>
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).

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Sep 6 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.