[已结束]CSDN论坛 第11期专家问答——LINQ开发答疑

CSDN 2013-08-01 04:03:15
加精
CSDN论坛本期专家问答我们请来了q107770540为大家解答LINQ开发相关问题。

问答时间:8月1日~8月8日

问答规则:LINQ开发相关都可以在此提问。

问答奖励:问答结束后会随机抽取3名用户,奖励最新一期《程序员》杂志一本。

专家简介

唐锦号

从事.NET相关项目开发多年,三届微软C# 方向MVP ,CSDN 社区专家;CSDN LINQ版版主. 具有多年的软件开发和项目管理经验,希望能一直写代码到老。


博客主页http://blog.csdn.net/q107770540

新浪微博http://weibo.com/tangjinhao



请大家踊跃提问吧,还有《程序员》杂志领噢~~
...全文
7811 131 打赏 收藏 转发到动态 举报
写回复
用AI写文章
131 条回复
切换为时间正序
请发表友善的回复…
发表回复
q107770540 2014-11-16
  • 打赏
  • 举报
回复
引用 130 楼 qq_19920727 的回复:
linq 我查询出一个对象集合 A 和一个 对象集合B 我想用in 的 去筛选 A中 包含B某部分对应条件的人应该怎么做
var query=A.Where(a=>B.Contains(a));
qq_19920727 2014-11-14
  • 打赏
  • 举报
回复
linq 我查询出一个对象集合 A 和一个 对象集合B 我想用in 的 去筛选 A中 包含B某部分对应条件的人应该怎么做
迷茫使者 2014-09-05
  • 打赏
  • 举报
回复
这个帖子 就这么没人了?我今天从头到尾看了下,受益匪浅 还得花点时间去取其所用!赞!
天空丶蒋 2013-12-31
  • 打赏
  • 举报
回复
菜鸟学习了。。。
_小黑_ 2013-11-14
  • 打赏
  • 举报
回复
pdf 的那个 打不开啊?能给我 发一份吗 397870376@qq.com
ActiveAndy 2013-10-04
  • 打赏
  • 举报
回复
好好学习,天天向上!
刘小吉 2013-08-18
  • 打赏
  • 举报
回复
只玩 Linq To Object 飘过
threenewbee 2013-08-17
  • 打赏
  • 举报
回复
引用 115 楼 q107770540 的回复:
你这不是95楼的问题么,一个字没变。。
不要理他,现在有不少用户怀疑用的自动灌水程序,随机从某一楼复制一行文字。
longqiwu 2013-08-13
  • 打赏
  • 举报
回复
请问哈,List<T> T表示引用类型,比如是一个类,Linq 如何去除此集合的重复数据.....................................................
tchqiq 2013-08-13
  • 打赏
  • 举报
回复
结束了啊?
q107770540 2013-08-13
  • 打赏
  • 举报
回复
你这不是95楼的问题么,一个字没变。。
韩小杰 2013-08-11
  • 打赏
  • 举报
回复
顶一个............
ytitxw 2013-08-10
  • 打赏
  • 举报
回复
学习学习
q107770540 2013-08-09
  • 打赏
  • 举报
回复
活动已结束
机器人 2013-08-08
  • 打赏
  • 举报
回复
引用 106 楼 caozhy 的回复:
[quote=引用 96 楼 q107770540 的回复:] [quote=引用 95 楼 binpo1 的回复:] 请问哈,List<T> T表示引用类型,比如是一个类,Linq 如何去除此集合的重复数据
重新实现IEqualityComparer<T>接口,使用Enumerable.Distinct<TSource> Method (IEnumerable<TSource>, IEqualityComparer<TSource>)来去重复:
public static IEnumerable<TSource> Distinct<TSource>(
	this IEnumerable<TSource> source,
	IEqualityComparer<TSource> comparer
)
举例,比如目前有这样一个Product[] 需要去重复:

public class Product
{
    public string Name { get; set; }
    public int Code { get; set; }
}


Product[] products = { new Product { Name = "apple", Code = 9 }, 
                       new Product { Name = "orange", Code = 4 }, 
                       new Product { Name = "apple", Code = 9 }, 
                       new Product { Name = "lemon", Code = 12 } };

首先要自定义一个class ProductComparer : IEqualityComparer<Product>
// Custom comparer for the Product class 
class ProductComparer : IEqualityComparer<Product>
{
    // Products are equal if their names and product numbers are equal. 
    public bool Equals(Product x, Product y)
    {

        //Check whether the compared objects reference the same data. 
        if (Object.ReferenceEquals(x, y)) return true;

        //Check whether any of the compared objects is null. 
        if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
            return false;

        //Check whether the products' properties are equal. 
        return x.Code == y.Code && x.Name == y.Name;
    }

    // If Equals() returns true for a pair of objects  
    // then GetHashCode() must return the same value for these objects. 

    public int GetHashCode(Product product)
    {
        //Check whether the object is null 
        if (Object.ReferenceEquals(product, null)) return 0;

        //Get hash code for the Name field if it is not null. 
        int hashProductName = product.Name == null ? 0 : product.Name.GetHashCode();

        //Get hash code for the Code field. 
        int hashProductCode = product.Code.GetHashCode();

        //Calculate the hash code for the product. 
        return hashProductName ^ hashProductCode;
    }

}
然后进行去重复操作:
IEnumerable<Product> noduplicates =
    products.Distinct(new ProductComparer());

foreach (var product in noduplicates)
    Console.WriteLine(product.Name + " " + product.Code);

/*
    This code produces the following output:
    apple 9 
    orange 4
    lemon 12
*/
[/quote] 哈哈,这种我一般都是 GroupBy(x => x.xxx).Select(x => x.First()) 不知道性能上会不会差一点。但是定义一个类真的很烦。[/quote] 顶个。还可以用自定义扩展方法: http://blog.csdn.net/fangxinggood/article/details/6187043
988kook 2013-08-08
  • 打赏
  • 举报
回复
人才
threenewbee 2013-08-08
  • 打赏
  • 举报
回复
引用 107 楼 fangxinggood 的回复:
[quote=引用 106 楼 caozhy 的回复:] [quote=引用 96 楼 q107770540 的回复:] [quote=引用 95 楼 binpo1 的回复:] 请问哈,List<T> T表示引用类型,比如是一个类,Linq 如何去除此集合的重复数据
重新实现IEqualityComparer<T>接口,使用Enumerable.Distinct<TSource> Method (IEnumerable<TSource>, IEqualityComparer<TSource>)来去重复:
public static IEnumerable<TSource> Distinct<TSource>(
	this IEnumerable<TSource> source,
	IEqualityComparer<TSource> comparer
)
举例,比如目前有这样一个Product[] 需要去重复:

public class Product
{
    public string Name { get; set; }
    public int Code { get; set; }
}


Product[] products = { new Product { Name = "apple", Code = 9 }, 
                       new Product { Name = "orange", Code = 4 }, 
                       new Product { Name = "apple", Code = 9 }, 
                       new Product { Name = "lemon", Code = 12 } };

首先要自定义一个class ProductComparer : IEqualityComparer<Product>
// Custom comparer for the Product class 
class ProductComparer : IEqualityComparer<Product>
{
    // Products are equal if their names and product numbers are equal. 
    public bool Equals(Product x, Product y)
    {

        //Check whether the compared objects reference the same data. 
        if (Object.ReferenceEquals(x, y)) return true;

        //Check whether any of the compared objects is null. 
        if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
            return false;

        //Check whether the products' properties are equal. 
        return x.Code == y.Code && x.Name == y.Name;
    }

    // If Equals() returns true for a pair of objects  
    // then GetHashCode() must return the same value for these objects. 

    public int GetHashCode(Product product)
    {
        //Check whether the object is null 
        if (Object.ReferenceEquals(product, null)) return 0;

        //Get hash code for the Name field if it is not null. 
        int hashProductName = product.Name == null ? 0 : product.Name.GetHashCode();

        //Get hash code for the Code field. 
        int hashProductCode = product.Code.GetHashCode();

        //Calculate the hash code for the product. 
        return hashProductName ^ hashProductCode;
    }

}
然后进行去重复操作:
IEnumerable<Product> noduplicates =
    products.Distinct(new ProductComparer());

foreach (var product in noduplicates)
    Console.WriteLine(product.Name + " " + product.Code);

/*
    This code produces the following output:
    apple 9 
    orange 4
    lemon 12
*/
[/quote] 哈哈,这种我一般都是 GroupBy(x => x.xxx).Select(x => x.First()) 不知道性能上会不会差一点。但是定义一个类真的很烦。[/quote] 顶个。还可以用自定义扩展方法: http://blog.csdn.net/fangxinggood/article/details/6187043 [/quote] 学习下~~我想问问你们公司内部有没有搞Linq扩展库,或者有没有这方面的开源项目推荐?因为我打算搞一个。
threenewbee 2013-08-07
  • 打赏
  • 举报
回复
引用 96 楼 q107770540 的回复:
[quote=引用 95 楼 binpo1 的回复:] 请问哈,List<T> T表示引用类型,比如是一个类,Linq 如何去除此集合的重复数据
重新实现IEqualityComparer<T>接口,使用Enumerable.Distinct<TSource> Method (IEnumerable<TSource>, IEqualityComparer<TSource>)来去重复:
public static IEnumerable<TSource> Distinct<TSource>(
	this IEnumerable<TSource> source,
	IEqualityComparer<TSource> comparer
)
举例,比如目前有这样一个Product[] 需要去重复:

public class Product
{
    public string Name { get; set; }
    public int Code { get; set; }
}


Product[] products = { new Product { Name = "apple", Code = 9 }, 
                       new Product { Name = "orange", Code = 4 }, 
                       new Product { Name = "apple", Code = 9 }, 
                       new Product { Name = "lemon", Code = 12 } };

首先要自定义一个class ProductComparer : IEqualityComparer<Product>
// Custom comparer for the Product class 
class ProductComparer : IEqualityComparer<Product>
{
    // Products are equal if their names and product numbers are equal. 
    public bool Equals(Product x, Product y)
    {

        //Check whether the compared objects reference the same data. 
        if (Object.ReferenceEquals(x, y)) return true;

        //Check whether any of the compared objects is null. 
        if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
            return false;

        //Check whether the products' properties are equal. 
        return x.Code == y.Code && x.Name == y.Name;
    }

    // If Equals() returns true for a pair of objects  
    // then GetHashCode() must return the same value for these objects. 

    public int GetHashCode(Product product)
    {
        //Check whether the object is null 
        if (Object.ReferenceEquals(product, null)) return 0;

        //Get hash code for the Name field if it is not null. 
        int hashProductName = product.Name == null ? 0 : product.Name.GetHashCode();

        //Get hash code for the Code field. 
        int hashProductCode = product.Code.GetHashCode();

        //Calculate the hash code for the product. 
        return hashProductName ^ hashProductCode;
    }

}
然后进行去重复操作:
IEnumerable<Product> noduplicates =
    products.Distinct(new ProductComparer());

foreach (var product in noduplicates)
    Console.WriteLine(product.Name + " " + product.Code);

/*
    This code produces the following output:
    apple 9 
    orange 4
    lemon 12
*/
[/quote] 哈哈,这种我一般都是 GroupBy(x => x.xxx).Select(x => x.First()) 不知道性能上会不会差一点。但是定义一个类真的很烦。
  • 打赏
  • 举报
回复
神奇的小猪 2013-08-07
  • 打赏
  • 举报
回复
引用 96 楼 q107770540 的回复:
[quote=引用 95 楼 binpo1 的回复:] 请问哈,List<T> T表示引用类型,比如是一个类,Linq 如何去除此集合的重复数据
重新实现IEqualityComparer<T>接口,使用Enumerable.Distinct<TSource> Method (IEnumerable<TSource>, IEqualityComparer<TSource>)来去重复:
public static IEnumerable<TSource> Distinct<TSource>(
	this IEnumerable<TSource> source,
	IEqualityComparer<TSource> comparer
)
举例,比如目前有这样一个Product[] 需要去重复:

public class Product
{
    public string Name { get; set; }
    public int Code { get; set; }
}


Product[] products = { new Product { Name = "apple", Code = 9 }, 
                       new Product { Name = "orange", Code = 4 }, 
                       new Product { Name = "apple", Code = 9 }, 
                       new Product { Name = "lemon", Code = 12 } };

首先要自定义一个class ProductComparer : IEqualityComparer<Product>
// Custom comparer for the Product class 
class ProductComparer : IEqualityComparer<Product>
{
    // Products are equal if their names and product numbers are equal. 
    public bool Equals(Product x, Product y)
    {

        //Check whether the compared objects reference the same data. 
        if (Object.ReferenceEquals(x, y)) return true;

        //Check whether any of the compared objects is null. 
        if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
            return false;

        //Check whether the products' properties are equal. 
        return x.Code == y.Code && x.Name == y.Name;
    }

    // If Equals() returns true for a pair of objects  
    // then GetHashCode() must return the same value for these objects. 

    public int GetHashCode(Product product)
    {
        //Check whether the object is null 
        if (Object.ReferenceEquals(product, null)) return 0;

        //Get hash code for the Name field if it is not null. 
        int hashProductName = product.Name == null ? 0 : product.Name.GetHashCode();

        //Get hash code for the Code field. 
        int hashProductCode = product.Code.GetHashCode();

        //Calculate the hash code for the product. 
        return hashProductName ^ hashProductCode;
    }

}
然后进行去重复操作:
IEnumerable<Product> noduplicates =
    products.Distinct(new ProductComparer());

foreach (var product in noduplicates)
    Console.WriteLine(product.Name + " " + product.Code);

/*
    This code produces the following output:
    apple 9 
    orange 4
    lemon 12
*/
[/quote]谢谢
加载更多回复(92)

8,497

社区成员

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

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