C# 可能会打开多个流(如果多次调用GetEnumerator)。我还对使用流(二进制数据)枚举字符(文本数据)感到不安。它应该是IEnumerable,或者使用文本阅读器而不是流。@plinth:哎呀,我指的是Func,不是“()=>流”:)只是一个注释。您的
C# 可能会打开多个流(如果多次调用GetEnumerator)。我还对使用流(二进制数据)枚举字符(文本数据)感到不安。它应该是IEnumerable,或者使用文本阅读器而不是流。@plinth:哎呀,我指的是Func,不是“()=>流”:)只是一个注释。您的,c#,linq,C#,Linq,可能会打开多个流(如果多次调用GetEnumerator)。我还对使用流(二进制数据)枚举字符(文本数据)感到不安。它应该是IEnumerable,或者使用文本阅读器而不是流。@plinth:哎呀,我指的是Func,不是“()=>流”:)只是一个注释。您的“where int.Parse(items[1])==24809”正在查看每行每列中的第二个字符。它以什么方式不处理TextReader?它在using语句中。(请注意,如果调用方手动调用MoveNext()然后放弃迭代器,则不会处理它,但f
可能会打开多个流(如果多次调用GetEnumerator)。我还对使用流(二进制数据)枚举字符(文本数据)感到不安。它应该是IEnumerable,或者使用文本阅读器而不是流。@plinth:哎呀,我指的是Func,不是“()=>流”:)只是一个注释。您的“where int.Parse(items[1])==24809”正在查看每行每列中的第二个字符。它以什么方式不处理TextReader?它在using语句中。(请注意,如果调用方手动调用MoveNext()然后放弃迭代器,则不会处理它,但foreach会自动调用dispose。)你说得对,我看错了。您没有使用接受Func的构造函数。我的意思是,当您使用该函数时,LineReader类不会完全封装TextReader实例的生存期。正如您的示例中所示,有两个“where”语句。如果第一个where语句不为true,它会继续读取linq语句还是保留它,并且myIntegers=null?@Bryan:否,即使您使用了接受Func的构造函数,my类也会对TextReader本身的生命周期负责—该函数仅在GetEnumerator调用中调用。我正在使用这个构造函数,从字符串构造函数链接而来。事实上,我只保留了一个Func而不是一个真正的文本阅读器,这使它更安全。
private void Filter(string filename)
{
using (TextWriter writer = File.CreateText(Application.StartupPath + "\\temp\\test.txt"))
{
using(TextReader reader = File.OpenText(filename))
{
string line;
while((line = reader.ReadLine()) != null)
{
string[] items = line.Split('\t');
int myInteger = int.Parse(items[1]);
if (myInteger == 24809) writer.WriteLine(line);
}
}
}
}
private void Filter(string filename)
{
using (TextWriter writer = File.CreateText(Application.StartupPath + "\\temp\\test.txt"))
{
using(TextReader reader = File.OpenText(filename))
{
string line;
while((line = reader.ReadLine()) != null)
{
string[] items = line.Split('\t');
var Linqi = from item in items
where int.Parse(items[1]) == 24809
select true;
if (Linqi == true) writer.WriteLine(line);
}
}
}
}
string[] items = { "12121", "2222", "24809", "23445", "24809" };
var Linqi = from item in items
where Convert.ToInt32(item) == 24809
select true;
if (Linqi.First() == true) Console.WriteLine("Got a true");
private void Filter(string filename)
{
using (TextWriter writer = File.CreateText(Application.StartupPath + "\\temp\\test.txt"))
{
var lines = File.ReadAllLines(filename);
var matches = from line in lines
let items = line.Split('\t')
let myInteger = int.Parse(items[1]);
where myInteger == 24809
select line;
foreach (var match in matches)
{
writer.WriteLine(line)
}
}
}
private void Filter(string filename)
{
using (TextWriter writer = File.CreateText(Application.StartupPath + "\\temp\\test.txt"))
{
using(TextReader reader = File.OpenText(filename))
{
List<string> lines;
string line;
while((line = reader.ReadLine()) != null)
lines.Add(line);
var query = from l in lines
let splitLine = l.Split('\t')
where int.Parse(splitLine.Skip(1).First()) == 24809
select l;
foreach(var l in query)
writer.WriteLine(l);
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
public sealed class LineReader : IEnumerable<string>
{
readonly Func<TextReader> dataSource;
public LineReader(string filename)
: this(() => File.OpenText(filename))
{
}
public LineReader(Func<TextReader> dataSource)
{
this.dataSource = dataSource;
}
public IEnumerator<string> GetEnumerator()
{
using (TextReader reader = dataSource())
{
string line;
while ((line = reader.ReadLine()) != null)
{
yield return line;
}
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
var query = from line in new LineReader(filename)
let items = line.Split('\t')
let myInteger int.Parse(items[1]);
where myInteger == 24809
select line;
using (TextWriter writer = File.CreateText(Application.StartupPath
+ "\\temp\\test.txt"))
{
foreach (string line in query)
{
writer.WriteLine(line);
}
}
var query = from line in new LineReader(filename)
where int.Parse(line.Split('\t')[1]) == 24809
select line;
var query = new LineReader(filename)
.Where(line => int.Parse(line.Split('\t')[1]) == 24809);
private IEnumerable<string> ReadLines(StreamReader reader)
{
while(!reader.EndOfStream)
{
yield return reader.ReadLine();
}
}
private void Filter(string fileName)
{
using(var writer = File.CreateText(Application.StartupPath + "\\temp\\test.txt"))
using(var reader = File.OpenText(filename))
{
var myIntegers =
from line in ReadLines(reader)
let items = line.Split('\t')
where items.Length > 1
let myInteger = Int32.Parse(items[1])
where myInteger == 24809
select myInteger;
foreach(var myInteger in myIntegers)
{
writer.WriteLine(myInteger);
}
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace LinqDemo
{
class Program
{
static void Main()
{
var baseDir = AppDomain.CurrentDomain.BaseDirectory;
File.WriteAllLines(
Path.Combine(baseDir, "out.txt"),
File.ReadAllLines(Path.Combine(baseDir, "in.txt"))
.Select(line => new KeyValuePair<string, string[]>(line, line.Split(','))) // split each line into columns, also carry the original line forward
.Where(info => info.Value.Length > 1) // filter out lines that don't have 2nd column
.Select(info => new KeyValuePair<string, int>(info.Key, int.Parse(info.Value[1]))) // convert 2nd column to int, still carrying the original line forward
.Where(info => info.Value == 24809) // apply the filtering criteria
.Select(info => info.Key) // restore original lines
.ToArray());
}
}
}
A1,2
B,24809,C
C
E
G,24809
B,24809,C
G,24809