locked
Error executing a LINQ query in C# RRS feed

  • Question

  • User-749382042 posted

    I am trying run the following script in code.     Note  that only LINQ query in LINQPad has no problem and generating the expected results. 

    var zzz = (from aa in _DBService.AvailableStocks
    join bb in _DBService.UserSelectedStocks.Where(c => c.UserID == UserID)
    on aa.StocksID equals bb.StocksID into newgroup
    from cc in newgroup.DefaultIfEmpty()
    select new
    {
    StocksID = aa.StocksID,
    Stock = aa.Stock,
    UserID = (cc.UserID == null ? "Null" : cc.UserID.ToString()),
    UserStockID = cc.UserStockID.ToString() == "" ? "Null" : cc.UserStockID.ToString(),
    StockID = (cc.StocksID == null ? "Null" : cc.StocksID.ToString())
    }).ToList();

    The error is 

    An exception of type 'System.NullReferenceException' occurred in Microsoft.EntityFrameworkCore.dll but was not handled in user code

    Additional information: Object reference not set to an instance of an object.

    Any help 

    Shahzad

    Sunday, April 2, 2017 11:27 AM

Answers

  • User-271186128 posted

    Hi shahzad73,

    An exception of type 'System.NullReferenceException' occurred in Microsoft.EntityFrameworkCore.dll but was not handled in user code

    Additional information: Object reference not set to an instance of an object.

    From your description, it seems that you are using EntityFrameworkCore. After create a .net core sample, it seems that the issue is related to the "cc". it is null.

    So, before check the property is null, we need to check whether the cc is null. Please try to use the following code:

                var userlist = _context.UserSelectedStocks.Where(c => c.UserID == 1).Select(c => c);
                var query = (from aa in _context.AvailableStocks
                             join bb in _context.UserSelectedStocks.Where(c => c.UserID == 1)
                             on aa.StocksID equals bb.StockID into newgroup
                             from cc in newgroup.DefaultIfEmpty()
                             select new
                             {
                                 StocksID = aa.StocksID,
                                 Stock = aa.Stock,
                                 UserID = cc != null? (cc.UserID == null ? "Null" : cc.UserID.ToString()):"Null",
                                 UserStockID = cc != null ? cc.UserStockID.ToString() == ""  ? "Null" : cc.UserStockID.ToString() : "Null",
                                 StockID = cc != null ? (cc.StockID.ToString() == "" ? "Null" : cc.StockID.ToString()) : "Null"
                             }).ToList();

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 3, 2017 10:27 AM

All replies

  • User-1509636757 posted

    Avoid using .ToString() in your query, it cannot handle Null values. Instead of using ToString(), you can use Convert.ToString() in your LinQ query.

    Sunday, April 2, 2017 1:49 PM
  • User-749382042 posted

    Yes that is correct     i got error with these statements in LINQ

    UserID = cc.UserID.ToString() == "" ? "Null" : Convert.ToString(cc.UserID)
    //UserStockID = cc.UserStockID.ToString() == "" ? "Null" : Convert.ToString(cc.UserStockID)
    //StockID = (cc.StocksID == null ? "Null" : Convert.ToString(cc.StocksID))

    These 3 fields are coming from another table which is JOINED with Stocks tables.    I have left join so stock table has all records but if no matching record is found in user selected stocks then these fields are NULL. if i remove these 3 then statement executes ok.    But I need to see what records are not found.  

    I did used Convert.ToString() but i am getting same error 

    Shahzad

    Sunday, April 2, 2017 5:08 PM
  • User-1509636757 posted

    Can you post you modified query where you are still facing the issue?

    Monday, April 3, 2017 6:43 AM
  • User-749382042 posted

    Query is exactly the same in the first post of this thread that you gave me in last previous post. The only minor change i did was to change the hard coded UserID with variable UserID. 

    Shahzad

    Monday, April 3, 2017 7:04 AM
  • User-1509636757 posted

    You should also change .ToString() to Convert.ToString(). I even doubt that writing C# conversion function inside LinQ query when you working with Entity Framework would work.

    Modified query you may try:

    var zzz = (from aa in _DBService.AvailableStocks
    join bb in _DBService.UserSelectedStocks.Where(c => c.UserID == UserID)
    on aa.StocksID equals bb.StocksID into newgroup
    from cc in newgroup.DefaultIfEmpty()
    select new
    {
    StocksID = aa.StocksID,
    Stock = aa.Stock,
    UserID = (cc.UserID == null ? "Null" : cc.UserID),
    UserStockID = cc.UserStockID == null ? "Null" : cc.UserStockID,
    StockID = (cc.StocksID == null ? "Null" : cc.StocksID)
    }).ToList();

    Monday, April 3, 2017 7:45 AM
  • User-271186128 posted

    Hi shahzad73,

    An exception of type 'System.NullReferenceException' occurred in Microsoft.EntityFrameworkCore.dll but was not handled in user code

    Additional information: Object reference not set to an instance of an object.

    From your description, it seems that you are using EntityFrameworkCore. After create a .net core sample, it seems that the issue is related to the "cc". it is null.

    So, before check the property is null, we need to check whether the cc is null. Please try to use the following code:

                var userlist = _context.UserSelectedStocks.Where(c => c.UserID == 1).Select(c => c);
                var query = (from aa in _context.AvailableStocks
                             join bb in _context.UserSelectedStocks.Where(c => c.UserID == 1)
                             on aa.StocksID equals bb.StockID into newgroup
                             from cc in newgroup.DefaultIfEmpty()
                             select new
                             {
                                 StocksID = aa.StocksID,
                                 Stock = aa.Stock,
                                 UserID = cc != null? (cc.UserID == null ? "Null" : cc.UserID.ToString()):"Null",
                                 UserStockID = cc != null ? cc.UserStockID.ToString() == ""  ? "Null" : cc.UserStockID.ToString() : "Null",
                                 StockID = cc != null ? (cc.StockID.ToString() == "" ? "Null" : cc.StockID.ToString()) : "Null"
                             }).ToList();

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 3, 2017 10:27 AM
  • User-1509636757 posted

    The point raised by Dillion, is the perfect one and I believe that must be the actual solution of Obj reference error in this case.. I would in addition also suggest to avoid using .ToString() method inside LinQ query.

    Monday, April 3, 2017 10:43 AM
  • User-749382042 posted

    Thanks a lot. finally the query runs in my projects. Here is some modification which are running in my system

    public List<UserStocksData> GetAllUserSelectedStocks(int UserID)
    {

    var zz = (from aa in _DBService.AvailableStocks
    join bb in _DBService.UserSelectedStocks.Where(c => c.UserID == 1)
    on aa.StocksID equals bb.StocksID into newgroup
    from cc in newgroup.DefaultIfEmpty()
    select new UserStocksData
    {
    StocksID = aa.StocksID,
    Stock = aa.Stock,
    UserID = cc != null ? (cc.UserID == null ? -1 : cc.UserID) : -1,
    UserStockID = cc != null ? (cc.UserStockID == null ? -1 : cc.UserStockID) : -1,
    StockID = cc != null ? (cc.StocksID == null ? -1 : cc.StocksID) : -1
    });

    return zz.ToList();
    }

    Shahzad 

    Monday, April 3, 2017 4:46 PM
  • User-749382042 posted

    Can somebody common on my this thread which is making me sick for some days now 

    https://forums.asp.net/t/2119021.aspx?All+test+cases+are+failing+with+BadImageFormatException+exception

    Thanks 

    Wednesday, April 5, 2017 7:32 AM