-1

LINQ to SQL を使用してデータセットを取得しています。このデータセットを次のようにフィルタリングする必要があります。

SourceName が null のフィールドが存在し、このフィールドに SourceName が null 以外のレコードが少なくとも 1 つある場合は、そのレコードを削除する必要があります。

それがその「フィールド」の唯一の行である場合、それはリストに残るはずです。

以下にデータの例を示します。データは 3 つの列で構成されています: 'Field'、'SourceName'、'Rate'

Field | SourceName |  Rate 
 10   |    s1      |   9   
 10   |    null    |   null
 11   |    null    |   null
 11   |    s2      |   5
 11   |    s3      |   4
 12   |    null    |   null
 13   |    null    |   null
 13   |    s4      |   7
 13   |    s5      |   8
  8   |    s6      |   2
  9   |    s7      |   23
  9   |    s8      |   9
  9   |    s9      |   3   

出力は次のようになります。

Field | SourceName | Rate 
 10   |  s1        |  9   
 11   |  s2        |  5
 11   |  s3        |  4
 12   |  null      |  null    //  <- (remains since there's only 
 13   |  s4        |  7       //      1 record for this 'Field')
 13   |  s5        |  8
  8   |  null      |  null
  9   |  s8        |  9
  9   |  s9        |  3     

フィルタリングするにはどうすればよいですか?

4

2 に答える 2

1

あなたが達成しようとしていることは些細なことではなく、.Where()節だけでは解決できません。フィルタ基準は、グループ化が必要な条件に依存するため、 を.GroupBy()使用してそのコレクションのコレクションをフラット化する必要があります.SelectMany()

次のコードは、LINQ to Objects を使用して期待される出力を満たしています。LINQ to SQL がそれを SQL に変換できない理由はわかりません。それほど難しいことは試していません。

        //Group by the 'Field' field.
yourData.GroupBy(x => x.Field)

        //Project the grouping to add a new 'IsUnique' field
        .Select(g => new { 
                        SourceAndRate = g,
                        IsUnique = g.Count() == 1,
        })

        //Flatten the collection using original items, plus IsUnique
        .SelectMany(t => t.SourceAndRate, (t, i) => new {
                        Field = t.SourceAndRate.Key,
                        SourceName = i.SourceName, 
                        Rate = i.Rate,
                        IsUnique = t.IsUnique
        })

        //Now we can do the business here; filter nulls except unique
        .Where(x => x.SourceName != null || x.IsUnique);
于 2013-01-09T00:48:27.620 に答える
0

ラムダの続きでLinqの組み込みの「Where」句を使用します。

ラムダと単純なPOCOクラスを使用して、データを次のようなリストに格納する単純な静的な例:

using System;
using System.Collections.Generic;
using System.Linq;

namespace Simple
{
    class Program
    {
        class Data
        {
            public string Field { get; set; }
            public string SourceName { get; set; }
            public string Rate { get; set; }
        }

        static List<Data> Create()
        {
            return new List<Data>
                {
                    new Data {Field = "10", SourceName = null, Rate = null},
                    new Data {Field = "11", SourceName = null, Rate = null},
                    new Data {Field = "11", SourceName = "s2", Rate = "5"}
                };
        }

        static void Main(string[] args)
        {
            var ls = Create();

            Console.WriteLine("Show me my whole list: \n\n");

            // write out everything
            ls.ForEach(x => Console.WriteLine(x.Field + "\t" + x.SourceName + "\t" + x.Rate + "\n"));


            Console.WriteLine("Show me only non nulls: \n\n");

            // exclude some things
            ls.Where(l => l.SourceName != null)
                .ToList()
                .ForEach(x => Console.WriteLine(x.Field + "\t" + x.SourceName + "\t" + x.Rate + "\n"));

            Console.ReadLine();
        }
    }
}
于 2013-01-08T21:59:39.627 に答える