473,387 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
3 1634
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: WebSnozz | last post by:
Some collections are such that efficient search algorithms work on them such as binary search if the collection is a type which is sorted. I'm wondering how LINQ searches these collections and if...
28
by: Marc Gravell | last post by:
In Linq, you can apparently get a meaningful body from and expression's .ToString(); random question - does anybody know if linq also includes a parser? It just seemed it might be a handy way to...
3
by: shapper | last post by:
Hello, I have 3 SQL tables: TagId, TagText PostId, TagId, ... FileId, TagId, ... I need to, using LINQ, select all records in Tags, including the columns TagId and TagText, but adding...
2
by: shapper | last post by:
Hello, I have two tables: TagId, PostId TagId, PostId How can I delete, given a TagId, the record from Tags and all records associated with it in PostTags.
2
by: d-42 | last post by:
Hi, I've got a many-many relationship between people and locations: Persons {personid, namefirst, accountid} Locations {locationid, locationname, accountid} // many-many join table...
24
by: luowan | last post by:
Hi I am currently building an application on .NetCF 3.5 in C#. The application needs to process xml files. I am thinking about using LINQ to process XML. I wrote a piece of code to modify the...
2
by: shapper | last post by:
Hello, I have the following LINQ: List<Tagtags = (from t in database.Tags select t).ToList(); I need to restrict my results and for that I have two parameters: StartWidth and N
6
by: Paulo | last post by:
Why LINQ should be used on my asp.net applications? I use C# 2.0, will I have to move to 3.0/3.5 just because LINQ? Tell me how to convert 2.0 code to LINQ, please. I just want to stay up to...
21
by: hrishy | last post by:
Hi Will LINQ be ported to Python ? regards Hrishy
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.