tags:

views:

277

answers:

4

Hi guys...

How should I use this in .NET 2.0 ...?

[DataObjectMethod(DataObjectMethodType.Select)]
public IEnumerable<OperatorField> FindByType(String type)
{
    //  return only selected type
    return (from ce in this.OperatorFields where ce.Type == type select ce).ToList();
}

I use this in a 3.5 projects, but now I'm adding new functionality to an old project that I cannot (at this time) upgrade to 3.5.


I just did this:

[DataObjectMethod(DataObjectMethodType.Select)]
public IEnumerable<OperatorField> FindByType(String type)
{
    //  return only selected type
    //return (from ce in this.OperatorFields where ce.Type == type select ce).ToList();

    List<OperatorField> r = new List<OperatorField>();

    foreach (OperatorField f in this.OperatorFields)
        if (f.Type == type)
            r.Add(f);

    return r;
}
+10  A: 

Can you still use C# 3.0 but not .NET 3.5? If so, keep the code as it is and use LINQBridge, which is LINQ to Objects implemented for .NET 2.0.

Otherwise, do this:

[DataObjectMethod(DataObjectMethodType.Select)]
public IEnumerable<OperatorField> FindByType(String type)
{
    List<OperatorField> list = new List<OperatorField>();
    foreach (OperatorField ce in OperatorFields)
    {
        if (ce.Type == type)
        {
            list.Add(ce);
        }
    }
    return list;
}
Jon Skeet
2.0 only :( at least until the next version so we can tell clients that they need to upgrade the framework. I just add what I think, a solution.
balexandre
+1 for LINQBridge.
Robert Koritnik
@balexandre: No, you mistake me. You can use Visual Studio 2008 and C# 3.0 features but still target .NET 2.0. You don't need your clients to upgrade the framework for that to work.
Jon Skeet
@balexandre: are you developing in VS 2008 or 2005? Jon's suggestion with LINQBridge actually makes it possible for you to run LINQ on .Net 2.0... But with a C#3 compiler that comes with VS2k8. So if you're developing in VS2k8, you can easily keep the code and add the assembly. It will work flawlessly on .net 2.0
Robert Koritnik
+1 for LINQBridge. Awesome!
Martinho Fernandes
using 2008 and targeting project to .NET Framework 2.0
balexandre
In that case you can use C# 3 - lambda expressions, query expressions etc. Use LINQBridge and you can keep the original code.
Jon Skeet
@Downvoter: Care to explain what you don't like about this answer?
Jon Skeet
+1  A: 

Something like this perhaps?

IList<OperatorField> col = new List<OperatorField>();
foreach (OperatorField f in this.OperatorFields)
{
    if (f.Type == type)
        col.Add(f);
}
return col;
cwap
+1  A: 
[DataObjectMethod(DataObjectMethodType.Select)]
public IEnumerable<OperatorField> FindByType(String type)
{
 foreach (OperatorField ce in this.OperatorFields)
 {
  if (ce.Type == type)
   yield return ce;
 }
}
bassfriend
A: 

Think about what the statement is doing, it's iterating over each item in the OperatorFields property, checking the type and then adding the item to a list before returning the completed list.

So you've got

Create new list
For each item in OperatorFields
  if item.Type equals type
    add item to list

return list
daz-fuller