I'm trying to get a row from my SQL Server database. I don't know how to get it I tried many ways but I coludn't get any good solution.
databasEntities db = new databasEntities();
var medlem = from medlemar in db.medlemar
where medlemar.namn == "Ali"
select medlemar;
Here is my database table Medlemar
:
medlem will be a collection of objects so you should be able to use foreach or .first on that.
foreach(var m in medlem)
{
var namn = m.namm;
...
}
or
var m1 = medlem.First();
In C#, var
is not a typeless type. It is actually a way of implicitly typing objects without having to type out the entire type name.
For example:
var someString = "Any String Value";
and
string someString = "Any String Value";
compile to exactly the same IL.
So in your case, the LINQ is returning an IEnumerable<T>
collection, where T is the type of your medlemar
object.
Since it is a collection, you need to access it just as you would any other collection. One possibility is using foreach
foreach(var m in medlem)
{
//Do Something
}
Another possibility is calling ToList()
and then accessing individual members by index:
var medlemList = medlem.ToList();
var namn = medlemList[i].namn; // where i is some specific index in the collection
Or if you just want to get the first object in the collection, you have several alternatives depending on your use case:
var firstMedlem = medlem.Single();
var firstMedlem = medlem.First();
var firstMedlem = medlem.SingleOrDefault();
var firstMedlem = medlem.FirstOrDefault();
Each of the above do roughly the same thing but will behave differently if the collection has zero or multiple objects.
Single()
will return the only object in a collection. If the collection contains multiple objects or zero, an exception will be thrown. Similarly, SingleOrDefault()
will throw an exception if there are multiple objects, but will return the value returned by default(T)
(usually null, except for when T is a value type).
First()
and FirstOrDefault()
also behave the same way where First()
will throw an exception if the collection is empty and FirstOrDefault()
will return the default value. They are different from the Single..()
methods in that they will always return the first member if a collection has multiple values.
So basically, FirstOrDefault()
will never throw an exception unless the collection itself is null. The other variations will throw an exception in one or more cases depending on the contents of the IEnumerable<T>
collection.
I think you need to use FirstOrDefault()
something like:
var medlem = from medlemar in db.medlemar
where medlemar.namn == "Ali"
select medlemar;
string nameFromVar = medlem.FirstOrDefault();
There are other options you can use besides FirstOrDefault()
:
Single
returns a single item like FirstOrDefault()
but it will throw an exception if there is either none or more than one item.
First
also returns a single item, but throw when there is no item.
But in case of the FirstOrDefault()
it will return the first item or return null when there is no item or more accurate:
default(TSource) if source is empty; otherwise, the first element in source.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.