Search code examples
c#linq-to-dataset

LINQ to Dataset no longer recognises the "Field<string>" keyword


I have some data in a dataset and I would normally select a record as shown below.

SnapshotDS.SnapshotRow[] previousRow = 
    m_PreviousSnapshot.Snapshot.Select(string.Format("TechKey = '{0}'", ChangeRow.TechKey)) 
        as SnapshotDS.SnapshotRow[];

ChangeRow.Prev_Staff_No = previousRow[0].Staff_No;

To do the same in LINQ, I would expect to do the following but I get a wiggly line under the Field key word.

var pRow = from p in m_PreviousSnapshot.Snapshot.AsEnumerable()
            where p.Field<string>("TechKey") == ChangeRow.TechKey
            .Single()
            select p;

ChangeRow.Prev_Staff_No = p.Staff_No;

I've used similar LINQ many times before and never had this problem.

Any suggestions?

EDIT

The compiler error I get is:

error CS0019: Operator '==' cannot be applied to operands of type 'string' and 'char'

... But the "TechKey field is definitely a string in the DataSet.


Solution

  • You likely need to add a reference to System.Data.DataSetExtensions.dll.

    The missing methods are actually extension methods.

    Also change:

    var pRow = from p in m_PreviousSnapshot.Snapshot.AsEnumerable()
            where p.Field<string>("TechKey") == ChangeRow.TechKey
            .Single()
            select p;
    

    to:

    var pRows = from p in m_PreviousSnapshot.Snapshot.AsEnumerable()
            where p.Field<string>("TechKey") == ChangeRow.TechKey
            select p;
    var pRow = pRows.Single();
    

    The .Single() (where you had it) is not doing what you think it is.