HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Transforming http request into linq query

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
linqintotransformingrequestqueryhttp

Problem

I wrote something that has probably done thousands of times:
a function that takes a parsed http query as input and return a linq query.

Any input is appreciated.

```
public IList GetLeads(NameValueCollection nvc)
{
IQueryable queryBase = QueryBase();

foreach (string key in nvc)
{
const string format = "dd-MM-yyyy";

if (nvc[key] == "all")
continue;

//case where an array of possible values is sent
const string regex = "\\[.*\\]";

Expression> predicate = c => false;

string[] values = nvc[key].Split(',');
foreach (string value in values)
{
Expression> predicateTemp;
DateTime date;
switch (Regex.Replace(key, regex, ""))
{
case "nature":
string value1 = value;
predicateTemp =
c => c.Typologies.NatureDemande == value1;
break;
case "departementCode":
string value2 = value;
predicateTemp = c => c.Geographies.DepartementCode == value2;
break;
case "start":
String start = value;
date = DateTime.ParseExact(start, format, null);

DateTime date1 = date;
predicateTemp = c => c.Temps.Date >= date1;
break;
case "end":
String end = value;
date = DateTime.ParseExact(end, format, null);

DateTime date2 = date;
predicateTemp = c => c.Temps.Date true;
break;
}
predicate = OrElse(predicate, predicateTemp);
}

queryBase = queryBase.Where(predicate);
}

//query all leads
IQueryable query = (from x in queryBase
select new Lead
{

Solution

Here's a stab at making it a tad more performant while simplifying the syntax a bit:

//case where an array of possible values is sent
private const string MyRegex = "\\[.*\\]";

private static readonly Regex replacer = new Regex(MyRegex, RegexOptions.Compiled);

public IList GetLeads(NameValueCollection nvc)
{
    var queryBase = QueryBase();

    foreach (var key in nvc.Cast().Where(key => nvc[key] != "all"))
    {
        // case where an array of possible values is sent
        Expression> predicate = c => false;

        foreach (var value in nvc[key].Split(','))
        {
            const string Format = "dd-MM-yyyy";
            Expression> predicateTemp;
            var localValue = value;

            switch (replacer.Replace(key, string.Empty))
            {
                case "nature":
                    predicateTemp = c => c.Typologies.NatureDemande == localValue;
                    break;
                case "departementCode":
                    predicateTemp = c => c.Geographies.DepartementCode == localValue;
                    break;
                case "start":
                    predicateTemp = c => c.Temps.Date >= DateTime.ParseExact(localValue, Format, null);
                    break;
                case "end":
                    predicateTemp = c => c.Temps.Date  true;
                    break;
            }

            predicate = this.OrElse(predicate, predicateTemp);
        }

        queryBase = queryBase.Where(predicate);
    }

    // query all leads
    return queryBase.Select(x => new Lead
    {
        id = Convert.ToInt32(x.Leads.DemandeWeb_FK),
        lng = Convert.ToDouble(x.Leads.longitudeClient.Replace(",", ".")),
        lat = Convert.ToDouble(x.Leads.latitudeClient.Replace(",", ".")),
        temps = Convert.ToInt32(x.Leads.GeolocDureeTrajetDistrib),
        distance = Convert.ToInt32(x.Leads.GeolocDistanceRouteDistrib),
        nature = x.Typologies.NatureDemande,
        type = x.Distributeurs.DistribType,
        reseau = x.Distributeurs.ReseauDistributeur,
        instance = x.Demands.IdInstance,
        distribution = Convert.ToInt32(x.Distributeurs.DistribIdPointDeVente),
    }).ToList();
}

Code Snippets

//case where an array of possible values is sent
private const string MyRegex = "\\[.*\\]";

private static readonly Regex replacer = new Regex(MyRegex, RegexOptions.Compiled);

public IList<Lead> GetLeads(NameValueCollection nvc)
{
    var queryBase = QueryBase();

    foreach (var key in nvc.Cast<string>().Where(key => nvc[key] != "all"))
    {
        // case where an array of possible values is sent
        Expression<Func<MyContext, bool>> predicate = c => false;

        foreach (var value in nvc[key].Split(','))
        {
            const string Format = "dd-MM-yyyy";
            Expression<Func<MyContext, bool>> predicateTemp;
            var localValue = value;

            switch (replacer.Replace(key, string.Empty))
            {
                case "nature":
                    predicateTemp = c => c.Typologies.NatureDemande == localValue;
                    break;
                case "departementCode":
                    predicateTemp = c => c.Geographies.DepartementCode == localValue;
                    break;
                case "start":
                    predicateTemp = c => c.Temps.Date >= DateTime.ParseExact(localValue, Format, null);
                    break;
                case "end":
                    predicateTemp = c => c.Temps.Date <= DateTime.ParseExact(localValue, Format, null);
                    break;
                default:
                    predicateTemp = c => true;
                    break;
            }

            predicate = this.OrElse(predicate, predicateTemp);
        }

        queryBase = queryBase.Where(predicate);
    }

    // query all leads
    return queryBase.Select(x => new Lead
    {
        id = Convert.ToInt32(x.Leads.DemandeWeb_FK),
        lng = Convert.ToDouble(x.Leads.longitudeClient.Replace(",", ".")),
        lat = Convert.ToDouble(x.Leads.latitudeClient.Replace(",", ".")),
        temps = Convert.ToInt32(x.Leads.GeolocDureeTrajetDistrib),
        distance = Convert.ToInt32(x.Leads.GeolocDistanceRouteDistrib),
        nature = x.Typologies.NatureDemande,
        type = x.Distributeurs.DistribType,
        reseau = x.Distributeurs.ReseauDistributeur,
        instance = x.Demands.IdInstance,
        distribution = Convert.ToInt32(x.Distributeurs.DistribIdPointDeVente),
    }).ToList();
}

Context

StackExchange Code Review Q#27800, answer score: 2

Revisions (0)

No revisions yet.