Skip to main content
deleted 126 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Optimize LINQ search with Custom Fixed Rankingcustom fixed ranking

I'veI have to perform on some collection and return ranking based on some logic I wish to optimize this working code since I think it can be bettered (maybe using Taskstasks?).

Here's my codeI need to search on Counterparts item. I load this data from DB and extension method that performsI've got more aliases per counterpart.

I need to return the searchresult based on those criteria:

  • Counterpart Code is equal to search string
  • Counterpart Code starts with search string
  • Counterpart Code contains search string
  • Counterpart Description contains search string
  • Counterpart Alias is equal to search string
  • Counterpart Alias starts with search string
  • Counterpart Alias contains search string

Each of those rules starts from Ranking 1 to 7 and I have to sort ok that ranking ascending.

Any suggestions? Thanks

#UPDATE #1

Here's a little bit of story about my problem.. I need to search on Counterparts item . I load those data from DB and I've got more aliases per counterpart.

I need to return the result based on those criteria :

  • Counterpart Code is equal to search string
  • Counterpart Code starts with search string
  • Counterpart Code cointains search string
  • Counterpart Description contains search string
  • Counterpart Alias is equal to search string
  • Counterpart Alias starts with search string
  • Counterpart Alias contains search string

Each of those rules starts from Ranking 1 to 7 and I have to sort ok that ranking ascending

Optimize LINQ search with Custom Fixed Ranking

I've to perform on some collection and return ranking based on some logic I wish to optimize this working code since I think it can be bettered (maybe using Tasks?)

Here's my code and extension method that performs the search

Any suggestions? Thanks

#UPDATE #1

Here's a little bit of story about my problem.. I need to search on Counterparts item . I load those data from DB and I've got more aliases per counterpart.

I need to return the result based on those criteria :

  • Counterpart Code is equal to search string
  • Counterpart Code starts with search string
  • Counterpart Code cointains search string
  • Counterpart Description contains search string
  • Counterpart Alias is equal to search string
  • Counterpart Alias starts with search string
  • Counterpart Alias contains search string

Each of those rules starts from Ranking 1 to 7 and I have to sort ok that ranking ascending

Optimize LINQ search with custom fixed ranking

I have to perform on some collection and return ranking based on some logic I wish to optimize this working code since I think it can be bettered (maybe using tasks?).

I need to search on Counterparts item. I load this data from DB and I've got more aliases per counterpart.

I need to return the result based on those criteria:

  • Counterpart Code is equal to search string
  • Counterpart Code starts with search string
  • Counterpart Code contains search string
  • Counterpart Description contains search string
  • Counterpart Alias is equal to search string
  • Counterpart Alias starts with search string
  • Counterpart Alias contains search string

Each of those rules starts from Ranking 1 to 7 and I have to sort ok that ranking ascending.

added 654 characters in body
Source Link
advapi
  • 175
  • 8

#UPDATE #1

Here's a little bit of story about my problem.. I need to search on Counterparts item . I load those data from DB and I've got more aliases per counterpart.

I need to return the result based on those criteria :

  • Counterpart Code is equal to search string
  • Counterpart Code starts with search string
  • Counterpart Code cointains search string
  • Counterpart Description contains search string
  • Counterpart Alias is equal to search string
  • Counterpart Alias starts with search string
  • Counterpart Alias contains search string

Each of those rules starts from Ranking 1 to 7 and I have to sort ok that ranking ascending

#UPDATE #1

Here's a little bit of story about my problem.. I need to search on Counterparts item . I load those data from DB and I've got more aliases per counterpart.

I need to return the result based on those criteria :

  • Counterpart Code is equal to search string
  • Counterpart Code starts with search string
  • Counterpart Code cointains search string
  • Counterpart Description contains search string
  • Counterpart Alias is equal to search string
  • Counterpart Alias starts with search string
  • Counterpart Alias contains search string

Each of those rules starts from Ranking 1 to 7 and I have to sort ok that ranking ascending

Source Link
advapi
  • 175
  • 8

Optimize LINQ search with Custom Fixed Ranking

I've to perform on some collection and return ranking based on some logic I wish to optimize this working code since I think it can be bettered (maybe using Tasks?)

Here's my code and extension method that performs the search

public class Counterpart
{
    public int Id { get; set; }

    public string Code { get; set; }

    public string Description { get; set; }

    public IEnumerable<Alias> Aliases { get; set; }

    public override bool Equals(object obj)
    {
        Counterpart obj2 = obj as Counterpart;
        if (obj2 == null) return false;
        return Id == obj2.Id;
    }
}

public class Alias
{
    public int? Type { get; set; }

    public string Description { get; set; }
}

internal class CounterPartRanking
{
    public int Rank { get; set; }

    public Counterpart CounterPart { get; set; }
}

public static class CounterpartExtensions
{
    public static IEnumerable<Counterpart> SearchWithRank(this IEnumerable<Counterpart> source, string pattern)
    {
        var items1 = source.Where(x => x.Code == pattern);
        var items2 = source.Where(x => x.Code.StartsWith(pattern));
        var items3 = source.Where(x => x.Code.Contains(pattern));
        var items4 = source.Where(x => x.Description.Contains(pattern));
        var items5 = source.Where(x => x.Aliases != null && x.Aliases.Any(y => y.Description == pattern));
        var items6 = source.Where(x => x.Aliases != null && x.Aliases.Any(y => y.Description.StartsWith(pattern)));
        var items7 = source.Where(x => x.Aliases != null && x.Aliases.Any(y => y.Description.Contains(pattern)));

        Stopwatch sw = Stopwatch.StartNew();
        var rankedItems = new List<CounterPartRanking>();


        if (items1.Any())
            rankedItems.AddRange(items1.Select(x => new CounterPartRanking { Rank = 1, CounterPart = x }));
        if (items2.Any())
            rankedItems.AddRange(items2.Select(x => new CounterPartRanking { Rank = 2, CounterPart = x }));
        if (items3.Any())
            rankedItems.AddRange(items3.Select(x => new CounterPartRanking { Rank = 3, CounterPart = x }));
        if (items4.Any())
            rankedItems.AddRange(items4.Select(x => new CounterPartRanking { Rank = 4, CounterPart = x }));
        if (items5.Any())
            rankedItems.AddRange(items5.Select(x => new CounterPartRanking { Rank = 5, CounterPart = x }));
        if (items6.Any())
            rankedItems.AddRange(items6.Select(x => new CounterPartRanking { Rank = 6, CounterPart = x }));
        if (items7.Any())
            rankedItems.AddRange(items7.Select(x => new CounterPartRanking { Rank = 7, CounterPart = x }));

        sw.Stop();

        Debug.WriteLine("Time elapsed {0} for {1}", sw.Elapsed, pattern);
        var items = rankedItems.OrderBy(x => x.Rank).Select(x => x.CounterPart);
        var distinct = items.Distinct();

        return distinct;
    }
}

Any suggestions? Thanks