简体   繁体   中英

Linq query returning null when trying to pass a column value from list object

Linq query returning null when trying to pass a column value fetched from list objects . Is it possible to do as done in the code. Expecting answer or some suggestion.

var query = from p in context.ProcessStepTables
            where (p.DiagramID == diagramInfo.DiagramID)
            orderby p.ProcessNo select new{
                  DiagramProcessID = p.DiagramProcessID,
                  ProcessNo = p.ProcessNo,
                  ProcessID = p.ProcessID,
                  ProcessName = Process().Find(x => 
                           p.ProcessID == x.ProcessID).ProcessName.ToString(),
                  MakerName = Maker().Find(x=>
                           p.MakerID==x.MakerID).MakerName.ToString(),
                  Price = p.Price,
                  Note = p.Note,
                  Notice = p.Notice
            };

private List<MakerTable> Maker()
{
   List<MakerTable> pList = new List<MakerTable>();
   try
   {
       IQueryable<MakerTable> maker = (from data in context.MakerTables
                                       select data) as IQueryable<MakerTable>;
       foreach (MakerTable val in maker)
       {
          pList.Add(val);
       }
       return pList.OrderBy(x => x.MakerName).ToList();

    }
    catch (Exception ex)
    {
       MessageBox.Show(ex.Message);
       return null;
    }
}

that is because, your provider doesn't know about .ToString() method ie, when you create a query in IQueryable form, it is translated into equivalent SQL query, so if you include any C# function, event a non-primitive datatype, it will throw you that error, because your query gets constructed something like below:

"Select s.DiagramProcessID as DiagramProcessID, ...other fields..
 from MakerTables s where something.ToString()=='anyvalue'"

so obviously, sql doesn't know anything about .ToString() .

Simply way to avoid is to, perform your custom Select, after applying .ToList() to your query.

When you do that or .AsEnumerable() , query is executed on the database and now whatever custom selection or where clause is there, is translated on the CLR

try this:

var query = context.ProcessStepTables
            .Where(s=>s.DiagramID == diagramInfo.DiagramID)
            .OrderBy(s=>s.ProcessNo)
            .ToList() //this will cause the query to be executed on the db
             //Now perform the selection on returned result set, now the linq
             //has to do with this dataset
            .Select(s=>new
             {
                   DiagramProcessID = s.DiagramProcessID,
                   ProcessNo = s.ProcessNo,
                   ProcessID = s.ProcessID,
                   //other items in your custom list
             });

and also you can replace your Maker method with below:

private List<MakerTable> Maker()
{
    try
    {
       return context.MakerTables.OrderBy(x=>x.MakerName).ToList();
    }
    catch (Exception ex)
    {
       MessageBox.Show(ex.Message);
       return null;
    }
}

Break into steps and check for nulls, step 1 is get the query going, then consumer needs to check for if Marker contains a valid value:

var query = from p in context.ProcessStepTables
            where (p.DiagramID == diagramInfo.DiagramID)
            orderby p.ProcessNo
            select new{
                        DiagramProcessID = p.DiagramProcessID,
                        ProcessNo = p.ProcessNo,
                        ProcessID = p.ProcessID,
                        ProcessName = Process().Find(x => p.ProcessID == x.ProcessID).ProcessName.ToString(),
                        Marker = context.MakerTables
                                        .OrderBy(itm => itm.MakerName)
                                        .FirstOrDefaut(itm => itm.MakerID==x.MakerID))
;

There is a situation where something isn't being found and the find is throwing an exception. It is better to check for null off of the above code on Marker before trying to extract a value from a property off of Marker.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM