C# .NET - erroe in linq

Asked By karthik karthik
02-Oct-10 01:38 AM

DataTable dt = ds.Tables[0];

DataTable dt1 = ds1.Tables[0];

 

DataRow[] intersection = dt.AsEnumerable().Intersect(dt1.AsEnumerable());

grvname.DataSource = intersection;

grvname.DataBind();


i am getting error

Error 1 Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<System.Data.DataRow>' to 'System.Data.DataRow[]'. An explicit conversion exists (are you missing a cast?) C:\Documents and Settings\vignaharikarthik.v\Desktop\Sample DropDown\DatasetMerge.aspx.cs 39 38 C:\...\Sample DropDown\

  Reena Jain replied to karthik karthik
02-Oct-10 02:22 AM
hi,

The signature of CopyToDataTable is as follows:

public static DataTable CopyToDataTable<T>(
    this IEnumerable<T> source
)
where T : DataRow

In your case, your statement dt = query2.CopyToDataTable<DataRow> is effectively saying that the source type is a DataRow, which it isn't. It is an anonymous type. Thus the error message indicating that you can't implicitly convert from AnonymousType to DataRow. Consider leaving the generic <T> qualifier off and let the inferencing take care of the typing for you as follows:

dt = query2.CopyToDataTable();

As for the Field<T>("fieldname") syntax, you should be able to use the strongly typed names in your query. It might help to check out our free chapter 14 which deals with LINQ to Datasets at http://www.manning.com/marguerie/.

hope this will help you
  Rohan Dave replied to karthik karthik
02-Oct-10 03:11 AM
The error is coming because you are trying to store IEnumarable to array of datarow.. modify your code so it will  return array of datarow in below way..


DataRow
[] intersection = dt.AsEnumerable().Intersect(dt1.AsEnumerable()).ToArray<DataRow>();

try this and let us know...

  Lalit M. replied to karthik karthik
02-Oct-10 03:29 PM
i think you retrieve via LINQ-to-SQL will be entity objects from your binding schema, and not directly compatible with ADO.NET DataRow objects unless you perform some kind of projection,you could bind your query directly to a DataGrid without resorting to ADO.NET DataTables http://msdn.microsoft.com/en-us/library/bb546190.aspx

http://msdn.microsoft.com/en-us/library/bb546190.aspx
Create New Account
help
a treemap view: VisualNDepend also offers a CQL query editor with intellisense and verbose syntax error description: Notice also that the NDepend report contains a diagram of assemblies' dependencies: CQL Queries Web.Configuration.BrowserCapabilitiesFactory Control 18456 System.Windows.Forms.Control DataGrid 15799 System.Windows.Forms.DataGrid DataTable 13597 System.Data.DataTable SELECT TOP 5 TYPES WHERE IsPublic ORDER BY NbMethods DESC Name # Methods Full Name BrowserCapabilitiesFactory System.Messaging.Design.QueuePathDialog XmlSerializationReader 355 System.Xml.Serialization.XmlSerializationReader HttpListenerResponse 321 System.Net.HttpListenerResponse DataTable 265 System.Data.DataTable HttpWebRequest 236 System.Net.HttpWebRequest SELECT TOP 5 TYPES ORDER BY TypeCa DESC Name Afferent Boolean) GetDefaultValueArguments(PrimitiveMapping, Object, CodeExpression&) 2 372 System.Xml.Serialization.XmlCodeExporter. GetDefaultValueArguments(PrimitiveMapping, Object, CodeExpression&) intersect(EndpointPermission) 2 344 System.Net.EndpointPermission. intersect(EndpointPermission) RaisePostBackEvent(String) 2 316 System.Web.UI.WebControls.WebParts. WebPartZoneBase.RaisePostBackEvent(String) . . . (47869 methods Windows.Forms.DataGridView Control 18456 System.Windows.Forms.Control DataGrid 15799 System.Windows.Forms.DataGrid DataTable 13597 System.Data.DataTable ListView 10875 System.Windows.Forms.ListView Form 10234 System.Windows.Forms
datarow in gridview using linq hai freinds so far i have used this coding to intersect the two table and bind it in the gridview through LINQ but it is not binding in the gridview what changes i should do? DataTable dt = ds.Tables[0]; DataTable dt1 = ds1.Tables[0]; DataRow [] intersection = dt.AsEnumerable Intersect(dt1.AsEnumerable()).ToArray< DataRow > (); grvname.DataSource = intersection; grvname.DataBind(); TRY something like this; DataTable dt = ds.Tables[0]; DataTable dt1 = ds1.Tables[0]; DataRow [] intersection = dt.AsEnumerable().Intersect(dt1.AsEnumerable()).ToArray< DataRow > (); DataTable dt1 = intersection . CopyToDataTable < DataRow > ( ) ; DataView dv = new DataView ( dt1 ) ; grvname. DataSource = dv ; grvname.DataBind(); hai
Error 91 in Autofilter Excel Hello! The following code filters the values in a column for Here's the question - after successfully looping through all visible rows, the code returns an error 91. As far as I can tell, it happens when it reaches the last visible In Range(PrefBAdd).SpecialCells(xlCellTypeVisible) ' If MyCell.Value = "y" Then MyCell.Offset(0, -13).Value = Intersect(Rows(MyCell.Row), BAdd).Value _ & " " & Intersect(Rows(MyCell.Row), BAdd.Offset(0, 1)).Value & " " _ & Intersect(Rows(MyCell.Row), BAdd.Offset(0, 2)).Value & " " _ & Intersect(Rows(MyCell.Row), BAdd.Offset(0, 3)).Value & " " _ Intersect(Rows(MyCell.Row), BAdd.Offset(0, 4)).Value MyCell.Offset(0, -8).Value = MyCell.Offset Next SrcWS.AutoFilterMode = False Excel Programming Discussions SrcWS.AutoFilterMode (1) SrcEntRng.AutoFilter (1) AutoFilter (1) Intersect (1) Error (1) FoundCell.Formula (1) FoundCell.Resize (1) FirstCol.Formula (1) hi, ! *IF*. . . your
working on remote site I have the config below in my web.config <customErrors defaultRedirect = "error / error.aspx" mode = "On"> <error statusCode = "500" redirect = "error / error.aspx" / > <error statusCode = "404" redirect = "error / error.aspx" / > < / customErrors> I have changed the mode to RemoteOnly with no success Initially i didn