LINQ 的一些聪明用法是什么?

17

除了LINQ to SQL之外,还有哪些巧妙的用途是LINQ可以应用到的?

你是否发现LINQ让一些问题变得更加容易解决了?请提供例子。

9个回答

19
  • 使用LINQ to Objects几乎可以轻松处理与集合相关的任何事情,我发现这比LINQ to SQL更加有用。
  • 我已经很喜欢Marc Gravell和我开发的一个框架,叫做Push LINQ(目前是MiscUtil的一部分,但可能最终会移到MoreLINQ)。这对于计算大规模流数据集上的聚合非常有用,例如巨大的日志文件。
  • LINQ to XML是我使用过的最好的XML API,并且与LINQ to Objects集成得非常好。
  • 并行LINQ是Parallel Extensions框架的一个非常好的部分-它确实使并行化适当的任务变得更加容易(虽然如果你不小心的话,它可能会出现hideously wrong

直到现在才读了Push LINQ的解释,可能稍后会用它来查看日志文件 :) - Joel Coehoorn

16

微软的Robert Shelton很友好地为我们列出了几个LINQ实现

截至2008年7月:

  • LINQ to Amazon - 亚马逊的LINQ
  • LINQ to Active Directory - 活动目录的LINQ
  • LINQ to Bindable Sources (SyncLINQ) - 可绑定源的LINQ(SyncLINQ)
  • LINQ over C- project - C项目中的LINQ
  • LINQ to CRM - 客户关系管理系统的LINQ
  • LINQ To Geo—Language Integrated Query for Geospatial Data - 用于地理空间数据的语言集成查询(LINQ To Geo)
  • LINQ to Excel - Excel的LINQ
  • LINQ to Expressions (MetaLinq) - 表达式的LINQ(MetaLinq)
  • LINQ Extender (Toolkit for building LINQ Providers) - 扩展LINQ的工具包(LINQ Extender)
  • LINQ to Flickr - Flickr的LINQ
  • LINQ to Google - 谷歌的LINQ
  • LINQ to Indexes (LINQ and i40) - 索引的LINQ(LINQ and i40)
  • LINQ to IQueryable (Matt Warren on Providers) - IQueryable的LINQ(Matt Warren在提供程序上)
  • LINQ to JSON - JSON的LINQ
  • LINQ to NHibernate - NHibernate的LINQ
  • LINQ to JavaScript - JavaScript的LINQ
  • LINQ to LDAP - LDAP的LINQ
  • LINQ to LLBLGen Pro - LLBLGen Pro的LINQ
  • LINQ to Lucene - Lucene的LINQ
  • LINQ to Metaweb(freebase) - Metaweb(freebase)的LINQ
  • LINQ to MySQL, Oracle and PostgreSql (DbLinq) - MySQL、Oracle和PostgreSql的LINQ(DbLinq)
  • LINQ to NCover - NCover的LINQ
  • LINQ to Opf3 - Opf3的LINQ
  • LINQ to Parallel (PLINQ) - 并行的LINQ(PLINQ)
  • LINQ to RDF Files - RDF文件的LINQ
  • LINQ to Sharepoint - Sharepoint的LINQ
  • LINQ to SimpleDB - SimpleDB的LINQ
  • LINQ to Streams - 流的LINQ
  • LINQ to WebQueries - Web查询的LINQ
  • LINQ to WMI - WMI的LINQ
  • LINQ to XtraGrid - XtraGrid的LINQ

13

我在单个C#语句中使用了LINQ来解决一些Project Euler问题。(请注意,语句和行不是同义词)

注意:小心邪恶的伎俩。

//Euler 1
//Add all the natural numbers below one thousand that are multiples of 3 or 5.
Enumerable.Range(0, 1000).Where(i => i % 5 == 0 || i % 3 == 0).Sum()

//Euler 2
//Find the sum of all the even-valued terms in the sequence which do not exceed four million
//Enumerable.Repeat(new List<long>(1024){ 1, 1 }, 1).First(fib => Enumerable.Range(0, int.MaxValue).TakeWhile(i => fib.Last() <= 4000000)
    .Aggregate(true, (u1, u2) => { fib.Add(fib.Last() + fib[fib.Count - 2]); return true; })).Where(n => n % 2 == 0).Sum()

//Euler 3 (>32bit)
//What is the largest prime factor of the number 600851475143?
Enumerable.Range(2, Int32.MaxValue - 2).Where(n => 600851475143 % n == 0 && Enumerable.Range(2, n / 2 - 1).All(f => n % f > 0)).Max()

//Euler 4
//Find the largest palindrome made from the product of two 3-digit numbers.
Enumerable.Range(100, 900).SelectMany(x => Enumerable.Range(100, 900).Select(y => x * y))
                          .Where(n => { var s = n.ToString(); return s.SequenceEqual(s.Reverse()); }).Max()

//Euler 5 (>32bit)
//What is the smallest number divisible by each of the numbers 1 to 20?
Enumerable.Range(20, Int32.MaxValue - 21).Where(n => Enumerable.Range(1, 20).All(i => n % i == 0)).First()

//Euler 6
//Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
Math.Pow(Enumerable.Range(1, 100).Sum(), 2) - Enumerable.Range(1, 100).Select(i => i * i).Sum()

//Euler 7
//Find the 10001st prime.
Enumerable.Range(2, Int32.MaxValue - 1).Where(n => Enumerable.Range(2, n / 2 - 1).All(f => n % f > 0)).Skip(10000).First()

//Euler 8
//Discover the largest product of five consecutive digits in the 1000-digit number.
Enumerable.Range(0, 995).Select(i => "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450"
    .Substring(i,5).Select(c => c - '0').Aggregate(1, (x, y) => x * y)).Max()


//Euler 10
//Find the sum of all the primes below two million.
Enumerable.Range(2, 2000000).Where(n => Enumerable.Range(2, n / 2 - 1).All(f => n % f > 0)).Select(x => (long)x).Sum()

Enumerable.Range(0, 168).Aggregate(Enumerable.Range(2, 2000000).Select(x => (long)x).ToList(), (result, index) => { result.RemoveAll(i => i > result[index] && i % result[index] == 0); return result; }).Sum()

Enumerable.Repeat(Enumerable.Range(2, 2000000).Select(x => (long)x).ToList(), 1).SelectMany(list => Enumerable.Range(0, Int32.MaxValue).Select(i => new { List = list, Index = i }))
    .TakeWhile((g, i) => g.List[g.Index] * g.List[g.Index] <= 2000000 || i.Dump("Rounds") != i).Aggregate((List<long>) null, (result, g) => { g.List.RemoveAll(i => i > g.List[g.Index] && i % g.List[g.Index] == 0); return g.List; }).Sum()

Enumerable.Repeat(Enumerable.Range(2, 2000000).Select(x => (long)x).ToList(), 1).First(list => Enumerable.Range(0, Int32.MaxValue)
    .TakeWhile(i => list[i] * list[i] <= 2000000 || i.Dump("Rounds")!=i).Aggregate(0, (count, i) => count + list.RemoveAll(j => j > list[i] && j % list[i] == 0)) != null).Sum()

//Euler 14
Enumerable.Range(1, 1000000).Select(s => Enumerable.Repeat(new List<long>(32) { s }, 1).First(list => Enumerable.Range(0, Int32.MaxValue).TakeWhile(i => list.Last() > 1)
    .Aggregate(0, (index, unused) => { list.Add(list.Last() % 2 == 0 ? list.Last() / 2 : 3 * list.Last() + 1); return 1; }) == 1 || true))
    .Aggregate(new List<long>(), (list, result) => list.Count <= result.Count ? result : list)

//Euler 19
//How many Sundays fell on the first of the month during the twentieth century?
Enumerable.Range(1901,100).SelectMany(y => Enumerable.Range(1,12).Select(m => new DateTime(y, m, 1))).Where(d => d.DayOfWeek == DayOfWeek.Sunday)

//Euler 21
//Evaluate the sum of all the amicable numbers under 10000.
Enumerable.Repeat(new Func<int, int>(n => Enumerable.Range(1, n / 2).Where(d => n % d == 0).Sum()), 1)
          .Select(D => Enumerable.Range(1, 10000).Where(a => a == D(D(a)) && a != D(a)).Sum())

//Euler 34
//Find the sum of all numbers which are equal to the sum of the factorial of their digits.
Enumerable.Range(3, 40600).Where(n => n == n.ToString().Select(d => Enumerable.Range(1, d - '0').Aggregate(1, (r, i) => r * i)).Sum()).Sum()

//Euler 40  
Enumerable.Repeat(new StringBuilder(), 1)
    .Where(result => Enumerable.Range(0, Int32.MaxValue)
                               .TakeWhile(i => result.Length <= 1000000)
                               .Aggregate(result, (unused, index) => result.Append(index)) != null)
    .Select(result => Enumerable.Range(1, 6).Select(i => result[(int)Math.Pow(10, i)] - '0')).First().Aggregate(1, (x, y) => x * y)

其他LINQ一行代码:

//Primes (Ineffecient)
Enumerable.Range(2, 1000000).Where(n => Enumerable.Range(2, n / 2 - 1).All(f => n % f > 0)).Count()

//Sieve of Eratosthenes
Enumerable.Range(0, 168)
          .Aggregate(Enumerable.Range(2, 1000000).ToList(), (result, index) => { 
               result.RemoveAll(i => i > result[index] && i % result[index] == 0); 
                return result; 
            }).Count
//Prime Factors
Enumerable.Range(2,13195 / 2)
          .Where(n => 13195 % n == 0
                   && Enumerable.Range(2, n / 2 - 1).All(f => n % f > 0))

//Fibonacci
Enumerable.Repeat(new List<long>(32){ 1, 1 }, 1)
    .First(fib => Enumerable.Range(0, 32).Aggregate(true, (u1, u2) => { 
        fib.Add(fib.Last() + fib[fib.Count - 2]); 
        return true; 
    }))

Euler8 稍作修改:Enumerable.Range(0, 1000 - seqLen).Select(i => digits.Substring(i, seqLen).Select(c => long.Parse(c.ToString())).Aggregate((x, y) => x * y)).Max(); - Dave Mateer

5
您还应该查看来自CodePlex网站的Bindable LINQ:http://www.codeplex.com/bindablelinq
“Bindable LINQ是一组扩展,可向LINQ添加数据绑定和更改传播功能。除了传播更改外,Bindable LINQ还可以在运行时分析查询并检测查询的任何依赖项。如果这些依赖项提供要订阅的事件,则Bindable LINQ将自动监视它们的更改。”
以下是该网站上的一个示例:
“以此查询为例:”
   contactsListBox.ItemsSource = from c in customers
                                 where c.Name.StartsWith(textBox1.Text)
                                 select c;

Bindable LINQ 将检测到查询依赖于 textBox1 对象的 Text 属性。由于 TextBox 是 WPF 控件,Bindable LINQ 知道要订阅控件的 TextChanged 事件。最终结果是随着用户输入,查询中的项目将被重新评估并且更改将显示在屏幕上。不需要编写额外的代码来处理事件,这一切都会自动完成。

4

我喜欢网站blog.functionalfun.net,因为它能够很好地介绍LINQ的实用(和不那么实用但更有趣的)用途。几乎他介绍的所有内容都可以应用于现实生活中,但他最近开始写更多“实用LINQ”主题的博客,介绍一些在业务逻辑代码中使用的有趣内容。


感谢你为我的博客做宣传,安东尼。我想我会为此投票支持你! - Samuel Jack

4

2
Linq to Excel”让从Excel电子表格中检索数据变得轻而易举。它负责创建OLEDB连接和SQL语句。你只需要告诉它电子表格的文件路径并创建Linq语句即可。

1

Chalie Calvert 博客列出了一些不错的Linq提供程序。

Nhibernate to Linq

我提到这个是因为它揭示了实现Linq提供程序以解决复杂问题所面临的许多挑战。


1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接