[visual-studio]linq to sql 中的删除启动与同一域的 URL

标签: Visual-Studio
发布时间: 2014/3/1 3:44:29
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有在数据表格中的 URL 的列表。我想要删除的行,从开始的相同的域。现在我们有此代码:

List<int> toRemove = new List<int>();
            toRemove.Clear();
            string initialDomain;
            string compareDomainName;
            for(int i = 0; i<UrlList.Rows.Count -1; i++)
            {
                if (toRemove.Contains(i))
                    continue;

                initialDomain = new Uri(UrlList.Rows[i][0] as String).Host;
                for(int j = i + 1; j < UrlList.Rows.Count; j++)
                {
                    compareDomainName = new Uri(UrlList.Rows[j][0] as String).Host;
                    if (String.Compare(initialDomain, compareDomainName, true) == 0)
                    {
                        toRemove.Add(j);
                    }
                }

                percent = i * 100 / total;
                if (percent > lastPercent)
                {
                    progress.EditValue = percent;
                    Application.DoEvents();
                    lastPercent = percent;

                }
            }

            for(int i = toRemove.Count-1; i>=0; i--)
            {
                UrlList.Rows.RemoveAt(toRemove[i]);
            }

它非常适合于小数据量,但当我加载长 Url 列表的速度很慢。现在我想要移动到 linq,但我不知道如何实现这一目标使用 linq。任何帮助吗?

更新*不需要对所有要删除 eduplicate 行。对于前男友。我有一个列表的 URL 现在,我知道如何删除重复的行。我的问题是: 我有一个简单的 url 的列表:

http://centroid.steven.centricagency.com/forms/contact-us?page=1544
http://chirp.wildcenter.org/poll
http://itdiscover.com/links/
http://itdiscover.com/links/?page=132
http://itdiscover.com/links/?page=2
http://itdiscover.com/links/?page=3
http://itdiscover.com/links/?page=4
http://itdiscover.com/links/?page=6
http://itdiscover.com/links/?page=8

http://www.foreignpolicy.com/articles/2010/06/21/la_vie_en
http://www.foreignpolicy.com/articles/2010/06/21/the_worst_of_the_worst
http://www.foreignpolicy.com/articles/2011/04/25/think_again_dictators
http://www.foreignpolicy.com/articles/2011/08/22/the_dictators_survival_guide
http://www.gsioutdoors.com/activities/pdp/glacier_ss_nesting_wine_glass/gourmet_backpacking/
http://www.gsioutdoors.com/products/pdp/telescoping_foon_orange/
http://www.gsioutdoors.com/products/pdp/telescoping_spoon_blue/ 

现在我想要此列表:

    http://centroid.steven.centricagency.com/forms/contact-us?page=1544
    http://chirp.wildcenter.org/poll
    http://itdiscover.com/links/
    http://www.foreignpolicy.com/articles/2010/06/21/la_vie_en
http://www.gsioutdoors.com/activities/pdp/glacier_ss_nesting_wine_glass/gourmet_backpacking/

解决方法 1:

var result = urls.Distinct(new UrlComparer());

public class UrlComparer : IEqualityComparer<string>
{
    public bool Equals(string x, string y)
    {
        return new Uri(x).Host == new Uri(y).Host;
    }

    public int GetHashCode(string obj)
    {
        return new Uri(obj).Host.GetHashCode();
    }
}

您还可以实现扩展方法DistinctBy

public static partial class MyExtensions
{
    public static IEnumerable<T> DistinctBy<T, TKey>(this IEnumerable<T> source, Func<T, TKey> keySelector)
    {
        HashSet<TKey> knownKeys = new HashSet<TKey>();
        return source.Where(x => knownKeys.Add(keySelector(x)));
    }
}

var result = urls.DistinctBy(url => new Uri(url).Host);
官方微信
官方QQ群
31647020