LINQ query problem, sequence contains no elements

I try to update a single record in the table, but when I run .Firstordefault() , I get the error: "The reference to the object is not installed in the instance of the object." If you use it with .First() , I get "The sequence contains no elements."

Using this other place, it works great, but this time it causes errors.

Here is the code:

 public class AllownceDetails { public int ta_id{get;set;} public int tvrid{get;set;} public DateTime ofDate{get;set;} public string status{get;set;} public string userid {get;set;} } //Update Method public void Update(AllownceDetails Allowncedtl) { var ta = (from a in ce.tbl_tvrallownce where a.tvrid == Allowncedtl.tvrid //error: Sequence contains no elements select a).SingleOrDefault(); ta.status = Allowncedtl.status; //error:Object reference not set to an instance of an object ce.SaveChanges(); } 
+4
source share
3 answers

The request should not return data. Run the profiler in the SQL database to see the physical query in progress and try to manually execute it on the database to see what the data looks like. You may need to tweak the query (or data) to get the results you are looking for.

A "sequence contains no elements" is basically a LINQ way of telling you that you are trying to reference an element from a list that has nothing. Therefore, calls to things like .First() or .Single() cannot find anything, therefore, an error.

When changing calls to something like .FirstOrDefault() or .SingleOrDefault() , then it will have a default value for this type, and for reference types, the default value is null . Therefore, if you set something to null , and then try calling the method on it, you will get an object reference not set to an instance of an object .

+15
source

The Single method throws this exception if there are no elements in the list (query) or if there are no several elements.

The SingleOrDefault method throws an exception if there are several items in the list. Returns null when there are no elements.

The FirstOrDefault method returns the first element in the list or null. There are no exceptions.

Use it and check the link for null

 if (ta != null ) { ta.status = Allowncedtl.status; ce.SaveChanges() } 

The source will always be the object, so do not worry about it in your case.

+5
source

All this means that your request does not match anything. Allowncedtl.tvrid is Allowncedtl.tvrid an identifier that matches nothing in the database. You should not assume that SingleOrDefault will return a nonzero value. Use SingleOrDefault only when you expect that there can be no value - and handle it. If a failure to find the value indicates an error, you should use Single (or perhaps First ).

We can’t tell anything about what is the main cause of your error: you must register the identifier you were looking for, find out why you were looking for it, and then check it in the database.

+3
source

Source: https://habr.com/ru/post/1403897/


All Articles