Comma delimited value Entity Framework in contains Statement - linq

I have LINQ statement that has a comma delimited value.
I want to see if my Field matches any of the comma delimited values.
public string IdentifyProductSKU(string Serial)
{
int Len = Serial.Length;
var Split = from ModelSplitter in entities.Models
select ModelSplitter.m_validationMask.Split(',');
var Product = (from ModelI in entities.Models
where ModelI.m_validation == 0 &&
ModelI.m_validationLength == Len &&
ModelI.m_validationMask.Contains(Serial.Substring(ModelI.m_validationStart, ModelI.m_validationEnd))
select ModelI.m_name).SingleOrDefault();
return Product;
}
To explain the code: Every Model has got multiple identifying properties for eg. XX1,XX5,XX7 is all the same product. Now when I pass in a serial number I want to Identify the product based on the validation mask. For eg: XX511122441141 is ProductA and YY123414124 is ProductC. I Just want to split the in this query so in this line:
ModelI.m_validationMask.Contains(Serial.Substring(ModelI.m_validationStart, ModelI.m_validationEnd))
I want to Split the Validation mask To see if the serial contains any of the validation mask characters. Does this make sense?

This is how you split values into a list
var split = context.Packs.Select(u => u.m_validationMask).ToList();
List<String[]> list=new List<String[]>();
foreach (var name in split)
{
String[] str = name.Split(',');
list.Add(str);
}
Now I need to know how I can use that list in my Final EF Query:
int Len = Serial.Length;
var split = entities.Models.Select(u => u.m_validationMask).ToList();
List<String[]> list = new List<String[]>();
foreach (var name in split)
{
String[] str = name.Split(',');
list.Add(str);
}
var Product = (from ModelI in entities.Models
where ModelI.m_validation == 0 &&
ModelI.m_validationLength == Len &&
list.Contains(Serial.Substring(ModelI.m_validationStart, ModelI.m_validationEnd))
select ModelI.m_name).SingleOrDefault();
return Product;

I don't fully understand what you mean or what you are trying to do. But...
If your ModelSplitter.m_malicationMask can indead be split as you had demonstrated, then Split is a List then. What I don't understand is if you are trying to match the entire product A, or just the first three characters, you can modifiy your query
var Product = (from ModelI in entities.Models
where ModelI.m_validation == 0 &&
ModelI.m_validationLength == Len &&
ModelI.m_validationMask.Contains(Serial.Substring(ModelI.m_validationStart, ModelI.m_validationEnd))
let productId = ModelI.m_name.Substring(0, 3)
where split.Contains(productId)
select ModelI.m_name).SingleOrDefault();
Product should now be null if it does not match or an acutal product if it does.

Related

Linq: Finding values within a range

I would like to match 1 Linq query with another one, based on a range provided.
For example, find all students with surnames between 'sa' and 'sn'. I'm looking to then find students with surnames Smith and Sammy, but not Swann and Anderson.
var allStudents = from s in Students select s;
var boundary = from b in boundaries select new { LowEnd = b.start, HighEnd = b.end }; //LowEnd = "sa" and HighEnd = "sn"
var matches = from s in allStudents
select new
{
s.Surname > boundary.LowEnd && s.Surname <= boundary.HighEnd
//This will obviously give a compile error, but not sure how to do it.
};
Since you are using LINQ to Objects, and assuming boundaries is a List<T> of conditions any one of which needs to be matched, you can test each student object from Students against each boundary:
var matches = from s in Students
where boundaries.Any(b => b.start.CompareTo(s.Surname) <= 0 && s.Surname.CompareTo(b.end) <= 0)
select s;
NOTE: Unfortunately C# doesn't have relational string operators and extensions anywhere isn't done, so you must use the CompareTo method.

try parse in linq

Hi I have a linq query as below
var perChange = (from data in globalDS.Tables[0].AsEnumerable().AsParallel()
select data.Field<double>("PercentChange")).ToList();
Now the dataset globalDS contains null as well as varchar values in them. So there is an obvious type cast error that is generated. Is there a way to try.Parse the value in "Percentage Change" column and select only the valid fields.
DataRow.Field supports nullable types:
List<double> result = globalDS.Tables[0].AsEnumerable().AsParallel()
.Where(r => r.Field<double?>("PercentChange").HasValue)
.Select(r => r.Field<double?>("PercentChange").Value)
.ToList();
Edit: Since you have mentioned that the field contains strings instead of doubles:
List<double> result = globalDS.Tables[0].AsEnumerable().AsParallel()
.Select(r => r.Field<string>("PercentChange").TryGetDouble())
.Where(nullDouble => nullDouble.HasValue)
.Select(nullDouble => nullDouble.Value)
.ToList();
I've used this extension to try-parse a string to double? which is safer than parsing "on the fly" into the local variable, especially with AsParallel:
public static Double? TryGetDouble(this string item, IFormatProvider formatProvider = null)
{
if (formatProvider == null) formatProvider = NumberFormatInfo.CurrentInfo;
Double d = 0d;
bool success = Double.TryParse(item, NumberStyles.Any, formatProvider, out d);
if (success)
return d;
else
return null;
}
How about this:
double temp;
var perChange = (
from data in globalDS.Tables[0].AsEnumerable().AsParallel()
where !data.IsNull("PercentChange")
&& double.TryParse(data.Field<string>("PercentChange"), out temp)
select double.Parse(data.Field<string>("PercentChange"))
).ToList();
Try a select using a where clause in which you check the type. This would be something like: where x != null && TypeOf(x) == double

Linq to Sql Query - better solution (optimizing)

The following code works, but it's not a nice code. (low performance)
I have a dictionary with value and key.
First i go trough every webcodes who exist. Then i load all participants in a list (where webcode equals the actual webcode in the foreach). After that i add the data (parameter of the webcode and a count of participants to the dictionary).
Guid compID = Guid.Parse(wID);
ChartModel webcodes = new ChartModel();
webcodes.Title = "Webcodes Statistics";
webcodes.Data = new Dictionary<string, int>();
var webcodesData = db.t_Webcode;
foreach (var w in webcodesData)
{
var wData = db.t_Participant.Where(t => t.FK_Competition == compID && t.Webcode == w.Webcode);
if (wData.Count() != 0)
webcodes.Data.Add(w.Parameter, wData.Count());
}
ViewBag.Webcodes = webcodes;
TIA
You need something along these lines:
webcodes.Data = (from w in db.t_Webcode
join p in db.t_Participant on w.Webcode equals p.Webcode
where p.FK_Competition == compID
group w by w.Parameter into g
select new { g.Key, Count = g.Count() }).ToDictionary();
I can't test it but that is the type of query you need.
This will assume that you have relationships defined in your database and that your LINQ to SQL datacontext are aware of them. If not, you will need to join manually on t_Participants from tWebcode.
This should execute in 1 single SQL query, instead of 1 query per row in tWebcode.
var webcodesAndNoOfParticipants =
from webcode in db.tWebcode
// Define number of participants for this webcode
let numberOfParticipants = webcode.t_Participants.Count(participant => participant.FK_Competition == compID)
where numberOfParticipants > 0
select new {
WebcodeParameter = webcode.Parameter,
NoOfParticipants = numberOfParticipants
};
webcodes.Data = webcodesAndNoOfParticipants.ToDictionary(x => x.WebcodeParameter, x => x.NoOfParticipants);

How to join results from two different sets in LINQ?

I get some data about customers in my database with this method:
public List<KlientViewModel> GetListOfKlientViewModel()
{
List<KlientViewModel> list = _klientRepository.List().Select(k => new KlientViewModel
{
Id = k.Id,
Imie = k.Imie,
Nazwisko = k.Nazwisko,
Nazwa = k.Nazwa,
SposobPlatnosci = k.SposobPlatnosci,
}).ToList();
return list;
}
but also I have another method which counts value for extra field in KlientViewModel - field called 'Naleznosci'.
I have another method which counts value for this field based on customers ids, it looks like this:
public Dictionary<int, decimal> GetNaleznosc(List<int> klientIds)
{
return klientIds.ToDictionary(klientId => klientId, klientId => (from z in _zdarzenieRepository.List()
from c in z.Klient.Cennik
where z.TypZdarzenia == (int) TypyZdarzen.Sprzedaz && z.IdTowar == c.IdTowar && z.Sprzedaz.Data >= c.Od && (z.Sprzedaz.Data < c.Do || c.Do == null) && z.Klient.Id == klientId
select z.Ilosc*(z.Kwota > 0 ? z.Kwota : c.Cena)).Sum() ?? 0);
}
So what I want to do is to join data from method GetNaleznosc with data generated in method GetListOfKlientViewModel. I call GetNaleznosc like this:
GetNaleznosc(list.Select(k => k.Id).ToList())
but don't know what to do next.
Having obtained the dictionary:
var dict = GetNaleznosc(list.Select(k => k.Id).ToList());
You can now efficiently look up the decimal value of Naleznosci for a given client:
foreach (var k in list)
k.Naleznosci = dict[k.Id];
Now you have merged the values into the main list. Is this what you mean?
By the way, in your function that builds the dictionary, you make it accept a List<int>, but then all you do is call ToDictionary on it, which only requires IEnumerable<int>. So change the parameter type to that, and then you can call it:
var dict = GetNaleznosc(list.Select(k => k.Id));
This removes the call to ToList, which avoids making an unnecessary intermediate copy of the whole list of Ids. Probably won't make much difference in this case if you're hitting a database and then building up a large set of results in memory, but maybe worth bearing in mind for other uses of these operations.
Also, looking again at the helper function, there is no apparent advantage in building up the set of results in a dictionary for a list of ids, because each one is handled independently. You could simply put:
public decimal GetNaleznosc(int klientId)
{
return (from z in _zdarzenieRepository.List()
from c in z.Klient.Cennik
where z.TypZdarzenia == (int) TypyZdarzen.Sprzedaz && z.IdTowar == c.IdTowar && z.Sprzedaz.Data >= c.Od && (z.Sprzedaz.Data < c.Do || c.Do == null) && z.Klient.Id == klientId
select z.Ilosc*(z.Kwota > 0 ? z.Kwota : c.Cena)).Sum() ?? 0);
}
That is, provide a function that discovers just one value. Now you can directly build the right list:
public List<KlientViewModel> GetListOfKlientViewModel()
{
return _klientRepository.List().AsEnumerable().Select(k => new KlientViewModel
{
Id = k.Id,
Imie = k.Imie,
Nazwisko = k.Nazwisko,
Nazwa = k.Nazwa,
SposobPlatnosci = k.SposobPlatnosci,
Naleznosci = GetNaleznosc(k.Id)
}).ToList();
}

Conditional Multiple Fields Searching and Filtering in LINQ

Assuming that we have the following table:
Person:
PersonID,
Name,
Age,
Gender
And we are providing a search function that allows users to search the table according to the name and/or the age.
The tricky part in writing the SQL ( or LINQ) query is that the users can choose to search for both field, or any one field, or no field. If he wants to search for all then he would just have to leave the textbox blank.
The logic to do this can be written as follows:
var p;
if(Name_TextBox=='')
{
p=from row in person
select row ;
}
else
{
p= from row in person
where row.Name=Name_TextBox
select row ;
}
// repeat the same for age
Now after a while the code gets very long and messy... How can I compress the above into a single query with no if-else?
Try code like this
string personName = txtPersonName.Text;
int personAge = Convert.ToInt32(txtAge.Text);
var opportunites = from p in this.DataContext.Persons
select new
{
p.PersonID,
p.Name,
p.Age,
p.Gender
};
if (personsID != 0)
opportunites = opportunites.Where(p => p.PersonID == personID);
if (personName != string.Empty)
opportunites = opportunites.Where(p => p.Name.StartsWith(personName));
if (personAge != 0)
opportunites = opportunites.Where(p => p.Age == personAge);
This will work fine. If personName is not given it will be not add to where, and if given then it will added.
One alternative which I have used in SQL which could be implemented in Linq too is
var p = from p in Person
where p.Name == Name_TextBox || Name_TextBox == String.Empty
select p;
(Note that your 'linq' is using SQL syntax, which won't compile. Also you can't declare a var as you are doing without directly assigning a value)
why not use the null coalescing operator? eg.
var products = from a in context.products
where a.ID == (productID ?? a.ID)
select a;
This works really well on my system

Resources