Linq SQL Help
-
Hi, I am new to Linq and I'm trying to access the MfgNumber after the query directly without a foreach loop. I don't need the foreach loop as the SQL query should be returning one result.
[Table(Name = "Mfg")]
public class Mfg
{
[Column(IsPrimaryKey = true)]
public string MfgName;
[Column]
public string MfgNumber;
}
...
var q2 =
from mfg in db.Mfg
where mfg.MfgName == Common.productPublisherName
select mfg;foreach (var mfg in q2) MfgNumber = mfg.MfgNumber;
How do I access the variables from the query without a foreach? Thank you,
Glenn
-
Hi, I am new to Linq and I'm trying to access the MfgNumber after the query directly without a foreach loop. I don't need the foreach loop as the SQL query should be returning one result.
[Table(Name = "Mfg")]
public class Mfg
{
[Column(IsPrimaryKey = true)]
public string MfgName;
[Column]
public string MfgNumber;
}
...
var q2 =
from mfg in db.Mfg
where mfg.MfgName == Common.productPublisherName
select mfg;foreach (var mfg in q2) MfgNumber = mfg.MfgNumber;
How do I access the variables from the query without a foreach? Thank you,
Glenn
This should meet your needs:
var q2 = from mfg in db.Mfg
where mfg.MfgName == Common.productPublisherName
select mfg.MfgNumber;foreach (var string mfgNumber in q2) {
....
}I just re-read your post and realized you expect there to be (at most) one entry, so Mark's answer is the one you want. /ravi
My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com
modified on Sunday, August 7, 2011 10:52 AM
-
Hi, I am new to Linq and I'm trying to access the MfgNumber after the query directly without a foreach loop. I don't need the foreach loop as the SQL query should be returning one result.
[Table(Name = "Mfg")]
public class Mfg
{
[Column(IsPrimaryKey = true)]
public string MfgName;
[Column]
public string MfgNumber;
}
...
var q2 =
from mfg in db.Mfg
where mfg.MfgName == Common.productPublisherName
select mfg;foreach (var mfg in q2) MfgNumber = mfg.MfgNumber;
How do I access the variables from the query without a foreach? Thank you,
Glenn
Use Single if the query will return a single result. q2 will be of type Mfg
var q2 =(from mfg in db.Mfg
where mfg.MfgName == Common.productPublisherName
select mfg).Single();or in case the query returns nothing create an object with default values, or null.
var q2 =(from mfg in db.Mfg
where mfg.MfgName == Common.productPublisherName
select mfg).SingleOrDefault();
I know the language. I've read a book. - _Madmatt
-
This should meet your needs:
var q2 = from mfg in db.Mfg
where mfg.MfgName == Common.productPublisherName
select mfg.MfgNumber;foreach (var string mfgNumber in q2) {
....
}I just re-read your post and realized you expect there to be (at most) one entry, so Mark's answer is the one you want. /ravi
My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com
modified on Sunday, August 7, 2011 10:52 AM
Ravi Bhavnani wrote:
foreach (var string mfgNumber in q2) {
The OP did say without for loop :rolleyes:
I know the language. I've read a book. - _Madmatt
-
Ravi Bhavnani wrote:
foreach (var string mfgNumber in q2) {
The OP did say without for loop :rolleyes:
I know the language. I've read a book. - _Madmatt
Mark Nischalke wrote:
The OP did say without for loop
The loop in my response processes the returned data - it doesn't extract it. /ravi
My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com
-
Mark Nischalke wrote:
The OP did say without for loop
The loop in my response processes the returned data - it doesn't extract it. /ravi
My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com
The only thing your code does different from the OPs is return a single property of the object rather than the object as a whole. LINQ queries are not executed until requested such as when the GetEnumerator method is called in a for each statement. So, yes your statement does extract the data, then processes it.
I know the language. I've read a book. - _Madmatt
-
The only thing your code does different from the OPs is return a single property of the object rather than the object as a whole. LINQ queries are not executed until requested such as when the GetEnumerator method is called in a for each statement. So, yes your statement does extract the data, then processes it.
I know the language. I've read a book. - _Madmatt
I just re-read the original post and realized he expects only one entry. :doh: My bad. /ravi
My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com