Convert LINQ to .NET 2.0

How to 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 3.5 projects, but now I am adding new features to an old project that I cannot (currently) 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;
}
+3
source share
4 answers

Is it possible to use C # 3.0 but not .NET 3.5? If so, save the code as is and use LINQBridge , which is LINQ to Objects, implemented for .NET 2.0.

Otherwise, do the following:

[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;
}
+12
source

Is something like this possible?

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

, , OperatorFields, .

,

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

return list
0

Source: https://habr.com/ru/post/1714402/


All Articles