tags:

views:

239

answers:

4

Hi.. new with Linq and very excited with this feature. However, I want to see if I can do the following with LINQ:

DataView source = (DataView) MyDataGrid.ItemsSource;

foreach (DataRowView vw in source)
 {
      if (vw.Row[dummyColumnIndex] != null && 
         vw.Row[dummyColumnIndex].ToString() == DisplayValue)
      {
            vw.Row[dummyColumnIndex] = string.Empty;
            break;
      }
 }

Notice that I have a break in my foreach loop, meaning that I just need to reset the first match row's column value to empty string.

Thanks!

+1  A: 
var firstMatch = source
    .Cast<DataRowView>()
    .First(vw => vw.Row[dummyColumnIndex] != null && vw.Row[dummyColumnIndex].ToString() == DisplayValue);
firstMatch.Row[dummyColumnIndex] = string.Empty;

Note that this will throw an exception if there's no match, so you could also do:

var firstMatch = source
    .Cast<DataRowView>()
    .FirstOrDefault(vw => vw.Row[dummyColumnIndex] != null && vw.Row[dummyColumnIndex].ToString() == DisplayValue);

if(firstMatch != null)
    firstMatch.Row[dummyColumnIndex] = string.Empty;
Lee
Yep, that's what I meant. :)
Mark Byers
+2  A: 

Yes it can. I tend to prefer the query syntax:

    var query = from vw in source.Cast<DataRowView>()
                where vw.Row[dummyColumnIndex] != null &&
                vw.Row[dummyColumnIndex].ToString() == DisplayValue
                select vw;

    var item = query.FirstOrDefault();
    if (item != null)
       item.Row[dummyColumnIndex] = "";

As noted by others: its best to use 'FirstOrDefault' to avoid throwing an exception if you don't find a match.

Matt Brunell
This won't actually work as written, because `source` is an `IEnumerable` and not an `IEnumerable<DataRowView>`. That's why the `.Cast<DataRowView>()` in Jesse's answer is required. Add that, and the query syntax should work just fine.
Joel Mueller
Okay, thanks. I've edited my answer to include that.
Matt Brunell
+4  A: 

Yes, but you'll need to use the Cast method of IEnumerable to cast this over to a generic IEnumerable where the Type argument is 'DataRowView', then you can do something like this. This will use the provided logic (in the form of a lambda expression) to evaluate each record in the DataView to determine if it's a match. It'll return the first one it finds, or 'null' if none are found. You can handle the null anyway you want; I usually like to throw exceptions in case like that unless there's a valid reason that no match might be found.

        var match = source.Cast<DataRowView>().FirstOrDefault(
                        s => s[dummyColumnIndex] != null && 
                            s[dummyColumnIndex].ToString() == DisplayValue);

        if (match == null)
          throw new Exception("Could not find match for " + DisplayValue);

        match[dummyColumnIndex] = String.Empty;
Jesse Taber
This is awesome! Thanks! So does LINQ essentially implicitly perform looping for me? Besides smaller line counts, does it actually increase performance over explicit looping like I had earlier as well? Thanks!
TheYouth
Essentially yes, LINQ is actually going to iterate over the collection for you, evaluating each item using the lamdba expression (the 's =>' syntax) and returning the first item that matches. As others pointed out, using 'FirstOrDefault' instead of First helps guard against a match not being found. I'm going to update the answer to include that.
Jesse Taber
If you do `(string)s[dummyColumnIndex]` instead of `s[dummyColumnIndex].ToString()` then you can leave out the null check for less code. A null will just be cast to a null string, which won't match `DisplayValue`.
Joel Mueller
Unless, of course, you actually need to call the .ToString() method of whatever happens to be in the DataRowView, because that object doesn't support casting to a string.
Joel Mueller
As Joel pointed out, Matt's answer will work fine if you call the 'Cast' method on the DataView. So if you prefer the query style syntax you could do that as well.
Jesse Taber
A: 

If the Objective is to find something in a list
{
if (sorted) http://en.wikipedia.org/wiki/Search%5Falgorithms
else if(efficent) http://en.wikipedia.org/wiki/Sort%5Falgorithms
else i don`t know
}

Behrooz