使用linq为什么缺少一些方法

welcome_me 2009-12-20 09:16:05
我在看网上使用LINQ的例子,但我的VS2008却没有那些方法,比如:
string[].EqualAll(string[])//没有EqualAll
int[].Fold()//没有Fold
像where,orderby这些是有的,为什么会缺少一些方法?
需要安装什么插件吗?
...全文
258 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
namhyuk 2009-12-20
  • 打赏
  • 举报
回复
不过,你改成如下,可能稍接近于它要表达的目的,除了Sum()操作以外,

下面这段测试过了。

static class Program
{
static void Main(string[] args)
{
LinqTest();
}
public static void LinqTest()
{
int[] vectorA = { 0, 2, 4, 5, 6 };
int[] vectorB = { 1, 3, 5, 7, 8 };
foreach (var num in vectorA.Combine(vectorB, (a, b) => a * b))
{
Console.WriteLine("Dot product: {0}", num);
}
Console.ReadLine();
}
public static IEnumerable<int> Combine(this IEnumerable<int> first, IEnumerable<int> second, Func<int, int, int> func)
{
var e1 = first.GetEnumerator();
var e2 = second.GetEnumerator();

while (e1.MoveNext() && e2.MoveNext())
yield return func(Convert.ToInt32(e1.Current), Convert.ToInt32(e2.Current));

}
}
namhyuk 2009-12-20
  • 打赏
  • 举报
回复
算了,我自己都有点乱了。就当我没回复吧。至于上面那些Func的例子倒是能运行的。
namhyuk 2009-12-20
  • 打赏
  • 举报
回复
稍等,我可能说错了,应该是Func<int, int>,其他的我先测试一下吧。
welcome_me 2009-12-20
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 namhyuk 的回复:]
Func <这里怎么写>
================
明摆着嘛, Func <接受的参数类型,返回值类型>
那就是Fuck <int[], int>呗,还能有什么?
[/Quote]
可是修改Func<int[],int>后8楼代码运行还是出错
using也有错:
“System.Collections.IEnumerator”: using 语句中使用的类型必须可隐式转换为“System.IDisposable”
namhyuk 2009-12-20
  • 打赏
  • 举报
回复
sorry,不是Fuck,是Func:-)
namhyuk 2009-12-20
  • 打赏
  • 举报
回复
Func <这里怎么写>
================
明摆着嘛, Func<接受的参数类型,返回值类型>
那就是Fuck<int[], int>呗,还能有什么?
welcome_me 2009-12-20
  • 打赏
  • 举报
回复
public void LinqTest()
{
int[] vectorA = { 0, 2, 4, 5, 6 };
int[] vectorB = { 1, 3, 5, 7, 8 };
int dotProduct = vectorA.Combine(vectorB, (a, b) => a * b).Sum();
Console.WriteLine("Dot product: {0}", dotProduct);
}
public static IEnumerable Combine(this IEnumerable first, IEnumerable second, Func<这里怎么写> func)
{
using (IEnumerator e1 = first.GetEnumerator(), e2 = second.GetEnumerator()){
while (e1.MoveNext() && e2.MoveNext())
yield return func(e1.Current, e2.Current);
}
}
namhyuk 2009-12-20
  • 打赏
  • 举报
回复

var h = new Hashtable { {"fuck", "fuck you"}, {"shit", "a piece of shit"}, {"dick", "suck my dick"} };

Func<Hashtable, string> fuck = f => { return f.Contains("fuck") ? f["fuck"].ToString() : "I don't give a fuck"; };

Label1.Text = fuck(h);


yizhili 2009-12-20
  • 打赏
  • 举报
回复
从没用过楼主说的两个方法哎
MSDN上也没查到

3楼的源代码是哪来的呢?.NET Framework里应该没有吧?
那楼主说的两个方法会不会是别人自己写的呢?
welcome_me 2009-12-20
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wuyq11 的回复:]
var a= new string[] { "a", "b" };
bool match = a.EqualAll("");
LINQ是否引用
[/Quote]

using System.Linq 已添加
有一些方法能出来,如:string[].Where()
有一些方法出不来,如:string[].EqualAll()
namhyuk 2009-12-20
  • 打赏
  • 举报
回复
有的.
Func<T, TResult) delegate

Encapsulates a method that has one parameter and returs a value of the type specified by the Tresult parameter.

using System;

public class LambdaExpression
{
public static void Main()
{
Func<string, string> fuck= s => s.ToUpper();
Console.WriteLine(fuck("fuck you"));
}
}
welcome_me 2009-12-20
  • 打赏
  • 举报
回复
扩展方法应该有实现该方法的代码呀,没找到

比如这个扩展方法:
public static IEnumerable Combine(this IEnumerable first, IEnumerable second, Func func)
{
using (IEnumerator e1 = first.GetEnumerator(), e2 = second.GetEnumerator())
{
while (e1.MoveNext() && e2.MoveNext())
{
yield return func(e1.Current, e2.Current);
}
}
}
就有实现的代码。但有一点不懂,C#中有 Func 类型吗?
wuyq11 2009-12-20
  • 打赏
  • 举报
回复
var a= new string[] { "a", "b" };
bool match = a.EqualAll("");
LINQ是否引用
namhyuk 2009-12-20
  • 打赏
  • 举报
回复
是不是扩展方法呀?

8,497

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 LINQ
社区管理员
  • LINQ
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧