How to get single value from linq query without using loop
-
I am using a linq query in a datatable for retrieving a single string value. Since it is a single value, I am looking into an option whether it is possible to retrieve the value without using foreach loop. Following is my code
string ejvqry = string.Empty;
var query = from frstqry in dsquery.Tables[0].AsEnumerable() select new { frstqrystring = frstqry.Field<string>("appendqry") };foreach (var fixedqry in query)
{
ejvqry = fixedqry.frstqrystring;
}Is there any way which I can get the value without foreach loop? I am sure that there will be only one value always. I tried to use Take, TakeWhile etc. But it is retrieiving some unknown characters. Please help. Thanks in advance.
Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.
-
I am using a linq query in a datatable for retrieving a single string value. Since it is a single value, I am looking into an option whether it is possible to retrieve the value without using foreach loop. Following is my code
string ejvqry = string.Empty;
var query = from frstqry in dsquery.Tables[0].AsEnumerable() select new { frstqrystring = frstqry.Field<string>("appendqry") };foreach (var fixedqry in query)
{
ejvqry = fixedqry.frstqrystring;
}Is there any way which I can get the value without foreach loop? I am sure that there will be only one value always. I tried to use Take, TakeWhile etc. But it is retrieiving some unknown characters. Please help. Thanks in advance.
Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.
I tend to use the
.SingleOrDefault();
for this.I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads
-
I am using a linq query in a datatable for retrieving a single string value. Since it is a single value, I am looking into an option whether it is possible to retrieve the value without using foreach loop. Following is my code
string ejvqry = string.Empty;
var query = from frstqry in dsquery.Tables[0].AsEnumerable() select new { frstqrystring = frstqry.Field<string>("appendqry") };foreach (var fixedqry in query)
{
ejvqry = fixedqry.frstqrystring;
}Is there any way which I can get the value without foreach loop? I am sure that there will be only one value always. I tried to use Take, TakeWhile etc. But it is retrieiving some unknown characters. Please help. Thanks in advance.
Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.
inject the "break;" into your foreach statement string ejvqry = string.Empty; var query = from frstqry in dsquery.Tables[0].AsEnumerable() select new { frstqrystring = frstqry.Field<string>("appendqry") }; foreach (var fixedqry in query){ ejvqry = fixedqry.frstqrystring; break; } Reference: http://www.programlive.tk have some C# simple example code here
-
inject the "break;" into your foreach statement string ejvqry = string.Empty; var query = from frstqry in dsquery.Tables[0].AsEnumerable() select new { frstqrystring = frstqry.Field<string>("appendqry") }; foreach (var fixedqry in query){ ejvqry = fixedqry.frstqrystring; break; } Reference: http://www.programlive.tk have some C# simple example code here
I have to ask: why? There's a perfectly suitable mechanism in place in Linq to support this - why not use the available tools to achieve this?
I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads
-
I tend to use the
.SingleOrDefault();
for this.I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads
-
It might be good to note that
.Single()
is more strict, it requires that you have exactly one value..SingleOrDefault();
is good when you want anull
when nothis is found and.Take(1)
is useful when you just want the first one no matter what.zommarin wrote:
.SingleOrDefault(); is good when you want a null when nothis is found
Not quite. If no element is found, and it's a none nullable type then the default is returned, e.g. an int would return 0.
I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads