English 中文(简体)
。 NET MVC 3 搜查控制器
原标题:.NET MVC 3 Search in controller

I m new to .NET. I m trying to do something very simple. I would like to perform a search on a Model in my controller and retrieve the first entity returned :

var cercueils = from y in db.Cercueils select y;
cercueils = cercueils.Where(z => z.Type.ToUpper().Contains(dr[13].ToUpper())
          || z.AncienType.ToUpper().Contains(dr[13].ToUpper()));
Cercueil cercueil = cercueils.First();

But this is not good, as it throws an error :

Le type de nœud « ArrayIndex » de l expression LINQ n est pas pris en charge dans LINQ to Entities.

页: 1

我如何能够做到这一点?

感谢您的帮助。

最佳回答

I believe Linq doesn t know how to execute dr[13] in the context of deferred execution... try the following:

var tmp = dr[13].ToUpper();
var cercueils = from y in db.Cercueils select y;
cercueils = cercueils.Where(z => z.Type.ToUpper().Contains(tmp)
          || z.AncienType.ToUpper().Contains(tmp));
Cercueil cercueil = cercueils.FirstOrDefault();

此外,我通常建议采用“第一号命令”,随后进行无效检查:

问题回答

我认为,Where(z =>z.Type.ToUpper(......)Contains(dr[13].ToUpper()等)不能被转至Kall类查询,因此Linq允许各实体给你上述错误。

You can fix the error in twi ways: either transform your expression so that Linq to Entities is able to generate the query (I had some success with IndexOf() instead of Contains()), or just download the entire table and perform the search locally (obviously, the second option will hamper performance):

var cercueils = (from y in db.Cercueils select y).ToList(); // load the entire dataset
cercueils = cercueils.Where(z => z.Type.ToUpper().Contains(dr[13].ToUpper())
      || z.AncienType.ToUpper().Contains(dr[13].ToUpper()));
Cercueil cercueil = cercueils.First();
        var cercueils = (from y in db.Cercueils
                        where y.blahblah = blahblah
                        select y).FirstOrDefault();




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...