-1

我正在使用 LINQ to SQL 获取数据集。我需要过滤这个数据集,这样:

如果存在具有空 SourceName 的字段,并且该字段至少有一个具有非空 SourceName 的其他记录,则应将其删除。

如果它是该“字段”的唯一行,那么它应该保留在列表中。

这是一个示例数据:数据由 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

Use Linq's built in 'Where' clause with a lambda continuation:

Simple static example of using lambda's and a simple POCO class to store the data in a list like yours:

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 回答