Simple LINQ question

  • Thread starter Thread starter Tony
  • Start date Start date
T

Tony

Hello!

I have a DataSet collection that have two columns named pkid and name. The
DataSet can look like this
pkid name
2 Admin
34 Dormant
23 Polen
26 Special

I just want to fetch pkid for the name Dormant. I have done so here by using
the var variable dormant in the result set.
I hope someone can tell me how I should write the linq query to get just the
pkid for name Dormant without using the var variable.
var dormant = from o in ds.Tables[0].AsEnumerable()
where o.Field<string>("Name").ToLower() ==
"dormant"
select o;

//Tony
 
Tony said:
Hello!

I have a DataSet collection that have two columns named pkid and name.
The DataSet can look like this
pkid name
2 Admin
34 Dormant
23 Polen
26 Special

I just want to fetch pkid for the name Dormant. I have done so here by
using the var variable dormant in the result set.
I hope someone can tell me how I should write the linq query to get just
the pkid for name Dormant without using the var variable.
var dormant = from o in ds.Tables[0].AsEnumerable()
where o.Field<string>("Name").ToLower() ==
"dormant"
select o;

Instead of selecting o select o.Field<int>("pkid") and take First e.g.
int pkid = (from o in ds.Tables[0].AsEnumerable()
where o.Field<string>("Name").ToLower() == "dormant"
select o.Field<int>("pkid")).First();

First will however throw an exception if there is no item in the sequence.
 
Tony said:
Hello!

I have a DataSet collection that have two columns named pkid and name.
The DataSet can look like this
pkid name
2 Admin
34 Dormant
23 Polen
26 Special

I just want to fetch pkid for the name Dormant. I have done so here by
using the var variable dormant in the result set.
I hope someone can tell me how I should write the linq query to get just
the pkid for name Dormant without using the var variable.
var dormant = from o in ds.Tables[0].AsEnumerable()
where o.Field<string>("Name").ToLower() ==
"dormant"
select o;

//Tony

var dormant = (from o in ds.Tables[0].AsEnumerable()
where o.Field<string>("Name").ToLower() == "dormant"
select o).FirstOrDefault();


if (dormant != null)
string id = dormant.pkid;
 
You can try this also:

int s = (from i in ds.Tables[0].AsEnumerable()
where i.Field<string>("Name").ToLower()
== "dormant"
select
i.Field<int>("pkid")).FirstOrDefault();

if (s > 0)
{
Console.WriteLine(s.ToString());
}
else
{
Console.WriteLine("Oops!");
}

Mr. Arnold said:
Tony said:
Hello!

I have a DataSet collection that have two columns named pkid and name.
The DataSet can look like this
pkid name
2 Admin
34 Dormant
23 Polen
26 Special

I just want to fetch pkid for the name Dormant. I have done so here by
using the var variable dormant in the result set.
I hope someone can tell me how I should write the linq query to get just
the pkid for name Dormant without using the var variable.
var dormant = from o in ds.Tables[0].AsEnumerable()
where o.Field<string>("Name").ToLower() ==
"dormant"
select o;

//Tony

var dormant = (from o in ds.Tables[0].AsEnumerable()
where o.Field<string>("Name").ToLower() == "dormant"
select o).FirstOrDefault();


if (dormant != null)
string id = dormant.pkid;
 
Back
Top