English 中文(简体)
如何在LINQ中使用WHERE? [闭门]
原标题:How to use WHERE in LINQ? [closed]
  • 时间:2012-05-15 10:24:35
  •  标签:
  • c#
  • linq

I am trying this but getting no success

var v=ds.Table[0].AsEnumerable().Where<DataRow>(r=>r.Field<int>("productID")==23);
GridView1.DataSource=v;
GridView1.DataBind();

网格显示,只有1个排向罗尔·哈斯罗尔,但我期望有1个排,有产品信息。

最佳回答

我认为,你应当尝试使用<条码>。 表格

ds.Table[0].AsEnumerable().Where<DataRow>(r=>r.Field<int>("productID")==23).CopyToDataTable();

When you convert it to an Enumerable the filtered list has no identifiers for say ProductID hence the grid would fail to map the column and fail.

也可选择使用<条码>AsDataView()而不是<条码>CopyToDataTable(<>>)。

A more detailed explanation can found Binding DataRows

Databinding is controlled by the ListBindingHelper and TypeDescriptor classes. When you bind to a list, the ListBindingHelper.GetListItemProperties method is called to get the columns in the list. If the list implements the ITypedList interface, its GetItemProperties method is called. Otherwise, it will use TypeDescriptor to get the properties of the first item in the list. (this uses reflection)

The DataView class (which DataTable also binds through, using IListSource) implements ITypedList and returns DataColumnPropertyDescriptors that expose the columns in the table. This is why you can bind to a DataView or DataTable and see columns. However, when you bind to a List, there is no ITypedList that can return the columns as properties. It therefore falls back on reflection and shows the physical properties of the DataRow class.

问题回答

引证。

var results = from myRow in ds.Table[0].Rows 
              where myRow.Field<int>("productID") == 23
              select myRow;

这应该给你看望的几句。





相关问题
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. ...

热门标签