0

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;
    }
}
4

2 回答 2

0

那是因为,您的提供者不知道.ToString()方法,即,当您以 IQueryable 形式创建查询时,它会被转换为等效的 SQL 查询,因此如果您包含任何 C# 函数,事件是非原始数据类型,它会抛出您该错误,因为您的查询构造如下:

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

很明显,sql 对.ToString().

.ToList()避免的简单方法是在应用到您的查询后执行您的自定义选择。

当你这样做时.AsEnumerable(),查询会在数据库上执行,现在任何自定义选择或 where 子句都会在 CLR 上进行翻译

尝试这个:

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
             });

你也可以用下面的方法替换你的 Maker 方法:

private List<MakerTable> Maker()
{
    try
    {
       return context.MakerTables.OrderBy(x=>x.MakerName).ToList();
    }
    catch (Exception ex)
    {
       MessageBox.Show(ex.Message);
       return null;
    }
}
于 2013-10-25T13:56:09.717 回答
0

分步检查空值,第 1 步是开始查询,然后消费者需要检查 Marker 是否包含有效值:

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))
;

有一种情况是没有找到某些东西并且发现抛出异常。在尝试从 Marker 的属性中提取值之前,最好先检查上述代码是否为空。Marker

于 2013-10-25T13:58:15.757 回答