Deal of the Day

Home » Main » Manning Forums » 2007 » LINQ in Action

Thread: Basic SQL to Linq question

Reply to this Thread Reply to this Thread Search Forum Search Forum Back to Thread List Back to Thread List

Permlink Replies: 3 - Pages: 1 - Last Post: Oct 30, 2009 7:46 PM by: fabrice.marguerie Threads: [ Previous | Next ]
PaoloTCS

Posts: 5
From: CT, USA
Registered: 3/14/08
Basic SQL to Linq question
Posted: Oct 30, 2009 1:46 PM
  Click to reply to this thread Reply

I have a table called Feeds with columns called FeedName, FeedLink and CompanyID. I
am trying with Linq to query the database table, and then place in a a label
the various values. I wish to select a FeedID, passed from the QueryString and use
that to filter the results from the table, and then use the various values,
so that is why I have labels everywhere as testers to see if I do get my values. Label1 works, but I can not get
other labels to work, for example FeedName, FeedID or CompanyID.


I have the following code:

private void DisplayNews()
{
// Get the data New Code Here to next comment ..............................

string FeedID = Request.QueryString["FeedID"];
TCSDataContext db = new TCSDataContext();
var newsFeedName = from n in db.Feeds
where n.FeedID == int.Parse(FeedID)
select n;
// select new {
// FeedName=n.FeedName,
// FeedLink=n.FeedLink,
// CompanyID=n.CompanyID
//};

Label1.Text = newsFeedName.FeedID;
Label2.Text = newsFeedName.FeedName;
Label3.Text = newsFeedName.FeedLink

Label1.Text = FeedID;
// End New Code .............................................................

But I am unable to access the values of the labesl, getting the error message,
"'System.Linq.IQueryable<Feed>' does not contain a definition for 'FeedID' and no extension method 'FeedID' accepting a first argument of type 'System.Linq.IQueryable<Feed>' could be found (are you missing a using directive or an assembly reference?)"
Any help would be appreciated
(BTW, the book is great!)

fabrice.marguerie


Posts: 225
From: France
Registered: 4/28/06
Re: Basic SQL to Linq question
Posted: Oct 30, 2009 3:54 PM   in response to: PaoloTCS in response to: PaoloTCS
  Click to reply to this thread Reply

Hello Paolo,

The problem lies in the type of newsFeedName. You probably agree that it's a query, right?
And that query selects a collection of Feeds. That's right, a collection, even if the "where" clause restricts this collection to one item at the maximum.
As the exception's message indicates, the type of newsFeedName is System.Linq.IQueryable<Feed>, and not Feed, as you want.
What you'll want to do is use either the Single or the SingleOrDefault query operator.

Another thing you'll have to do is to convert the FeedID string into an int outside of the query. The query must be convertible into SQL, and int.Parse(String) can't be converted.

Here is what I suggest:

string feedIDString = Request.QueryString["FeedID"];
int feedID = int.Parse(FeedID);
TCSDataContext db = new TCSDataContext();
IQueryable<Feed> feeds =
from feed in db.Feeds
where feed.FeedID == feedID
select new { feed.FeedName, feed.FeedLink, feed.CompanyID };
Feed feed = feeds.SingleOrDefault();
if (feed == null)
throw new Exception("Feed not found: FeedID = "+feedID);

Label1.Text = feedID.ToString();
Label2.Text = feed.FeedName;
Label3.Text = feed.FeedLink;

Is this ok?

Regards,
Fabrice

PaoloTCS

Posts: 5
From: CT, USA
Registered: 3/14/08
Re: Basic SQL to Linq question
Posted: Oct 30, 2009 6:54 PM   in response to: fabrice.marguerie in response to: fabrice.marguerie
  Click to reply to this thread Reply

Thank you, Fabrice,

I still get errors :

I changed one line that seemed like a typo:
int feedID = int.Parse(FeedID); to
int feedID = int.Parse("FeedID");

But still had errors at lines ...
select new ....
(Error is: CS0266: Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#1>' to 'System.Linq.IQueryable<Feed>'. An explicit conversion exists (are you missing a cast?)

and the next line Feed feed (the second feed has squiggly) with message: a local varible named feed cannot be declared in this scope becaues it would give a different meaning to feed, which is already used in a child scope to denote something else.

No more squgglies under the labels, however.

Merci bien,

Paolo



string feedIDString = Request.QueryString["FeedID"];
int feedID = int.Parse("FeedID");
TCSDataContext db = new TCSDataContext();
IQueryable<Feed> feeds =
from feed in db.Feeds
where feed.FeedID == feedID
select new { feed.FeedName, feed.FeedLink, feed.CompanyID };
Feed feed = feeds.SingleOrDefault(); if (feed == null) throw new Exception("Feed not found: FeedID = " + feedID);
Label1.Text = feedID.ToString();
Label2.Text = feed.FeedName;
Label3.Text = feed.FeedLink;

fabrice.marguerie


Posts: 225
From: France
Registered: 4/28/06
Re: Basic SQL to Linq question
Posted: Oct 30, 2009 7:46 PM   in response to: PaoloTCS in response to: PaoloTCS
  Click to reply to this thread Reply

Sorry for the mistakes. This was written too quickly.

1) This won't give you a very good result neither: int feedID = int.Parse("FeedID");
Try with int feedID = int.Parse(feedIDString);

2) We need to use var because we use an anonymous type.

3) I hate this kind of scope issue. It's too bad this problem exists. Whatever, let's use different names.

Here is a new version:

string feedIDString = Request.QueryString["FeedID"];
int feedID = int.Parse(feedIDString);
TCSDataContext db = new TCSDataContext();
IQueryable<Feed> feeds =
from f in db.Feeds
where f.FeedID == feedID
select new { f.FeedName, f.FeedLink, f.CompanyID };
var feed = feeds.SingleOrDefault();
if (feed == null)
throw new Exception("Feed not found: FeedID = "+feedID);

Label1.Text = feedID.ToString();
Label2.Text = feed.FeedName;
Label3.Text = feed.FeedLink;


Legend
Gold: 300 + pts
Silver: 100 - 299 pts
Bronze: 25 - 99 pts
Manning Author
Manning Staff
Manning Developmental Editor