Sorry, it was difficult to choose a good title for the doubt.
I'm querying my Repository using LINQ on a specific column string called "Parameter" that have ';' separator. The database column it's a pattern like "name1=value1;name2=value2;name3=value3" and I'm able to convert it to an Object called GenericParameter[] spliting the string by ';'.
My GenericParameter class that deal with Parameter looks like this:
public class GenericParameter
{
public string Name {get; set;} //name1
public string Value {get; set;}//value1
public static GenericParameter[] GetParameters(string parameters)
{
List<GenericParameter> chargingParameterList = new List<GenericParameter>();
if (!string.IsNullOrEmpty(parameters))
{
string[] splitedChargingParameter = parameters.Trim().Split(';');
foreach (string parameter in splitedChargingParameter)
{
string[] pair = parameter.Split('=');
if (pair.Length != 2) continue;
GenericParameter genericParameter = new GenericParameter()
{
Name = pair[0].Trim(),
Value = pair[1].Trim()
};
chargingParameterList.Add(genericParameter);
}
}
return chargingParameterList.ToArray();
}
}
The method I'm working on looks like this now:
public ICollection<MyData> GetMyDataByParameter(string param)
{
//param="name1=value1;name2=value2"
List<MyData> dataToReturn = context
.MyDataRepository
.AsQueryable()
.Where(p => p.ParameterStr.Contains(param))
.ToList();
return dataToReturn;
}
I'm refactoring it, because in this way my "param" can be passed in different orders like "name2=value2;name1=value1" and may not match the database column Parameter all the time.
I'm trying to do something like this:
public ICollection<MyData> GetMyDataByParameter(string param)
{
GenericParameter[] receivedParamArray = GenericParameter.GetParameters(param);
//param="name1=value1;name2=value2"
//GenericParameter[0].Name is "name1"
//GenericParameter[0].Value is "value1"
// TODO: QUERY the records on repository that matchs all the
//receivedParamArray
List<MyData> dataToReturn = context
.AsQueryable()
//.Where(p => p.ParameterStr.Contains(receivedParamArray))
.ToList();
return dataToReturn;
}
How can I query the repository column Parameter (string) that matchs all receivedParamArray. The query represented by the commented Where will not work.
Please, I appreciate any suggestions.
Dictionary<string,string>. No need to reinvent the wheel again.