I was doing mapping with wildcards. I get this error because the definition of the rule to be searched only comes from a table.
I do not know exactly how right I am doing this here, but as a result I have a mistake and I expect your help.
public class Ekstre
{
private readonly DataClasses1DataContext db = new DataClasses1DataContext();
private readonly KdrGnyClassesDataContext kg = new KdrGnyClassesDataContext();
public bool check { get; set; }
public int Id { get; set; }
public DateTime Tarih { get; set; }
public string Kodu { get; set; }
public string Açıklama { get; set; }
public decimal Tutar { get; set; }
public string bankaKod { get; set; }
private string kod = null;
public string muhKod {
get { return kod = kg.kuralTanimlari
.FirstOrDefault(a => Regex.IsMatch(Açıklama, WildCardToRegular(a.kural))).hesapKodu;
}
set { kod = value; }
}
private string hesap = null;
public string hesapAdi {
get {
hesap = !string.IsNullOrWhiteSpace(muhKod) ? db.MUHHESAP.First(p => p.MUHKOD == muhKod).MUHADI1 : null;
return hesap;
}
set => hesap = value;
}
public string kodTipi { get; set; }
}
public static string WildCardToRegular(string value)
{
return "^" + Regex.Escape(value).Replace("\\?", ".").Replace("\\*", ".*") + "$";
}
You can only use supported functions on dbcontext queries over IQuerable.
You have two options
1. Fire db query by doing ToList() or ToArray() and then .FirstOrDefault(.....)
This has a drawback that it will fetch all records from db and then apply your
function to results.
Convert your filtering function to a simple sql supported function. As far as i know Regex is not supported in entity framework
public string muhKod {
get
{
var firstOrDefault = kg.kuralTanimlari.ToList()
.FirstOrDefault(a => Regex.IsMatch(Açıklama, WildCardToRegular(a.kural)));
if (firstOrDefault != null)
kod = firstOrDefault.hesapKodu;
return kod;
}
set => kod = value;
}
It works so beautifully. Thank you.
Related
I want to compare all of my list of generic T property value to my local variable searchText.
i already try:
x.GetType().GetProperties().All(props => props.GetValue(x).ToString().ToLower().Contains(searchText.ToLower()))
and i get error : NullReferenceException: Object reference not set to an instance of an object.
here my full method :
protected List<T> ProcessCollection<T>(List<T> lstElements, IFormCollection requestFormData, Func<T, IComparable> getProp)
{
string searchText = string.Empty;
Microsoft.Extensions.Primitives.StringValues tempOrder = new[] { "" };
if (requestFormData.TryGetValue("search[value]", out tempOrder))
{
searchText = requestFormData["search[value]"].ToString();
}
var skip = Convert.ToInt32(requestFormData["start"].ToString());
var pageSize = Convert.ToInt32(requestFormData["length"].ToString());
if (requestFormData.TryGetValue("order[0][column]", out tempOrder))
{
var columnIndex = requestFormData["order[0][column]"].ToString();
var sortDirection = requestFormData["order[0][dir]"].ToString();
tempOrder = new[] { "" };
if (requestFormData.TryGetValue($"columns[{columnIndex}][data]", out tempOrder))
{
var columName = requestFormData[$"columns[{columnIndex}][data]"].ToString();
if (pageSize > 0)
{
var prop = GetProperty<T>(columName);
if (sortDirection == "asc")
{
return lstElements.Where(x=>x.GetType().GetProperties().All(props => props.GetValue(x).ToString().ToLower().Contains(searchText.ToLower()))
.Skip(skip).Take(pageSize).OrderBy(b => prop.GetValue(b)).ToList();
}
else
return lstElements
.Where(x => getProp(x).ToString().ToLower().Contains(searchText.ToLower()))
.Skip(skip).Take(pageSize).OrderByDescending(b => prop.GetValue(b)).ToList();
}
else
return lstElements;
}
}
return null;
}
this is how i call my method :
var listItem = ProcessCollection<Jtabel>(temp,requestFormData,x=>x.Name);
the temp that i pass to method already filled
and this is the class type that i pass to this method
public class Jtabel : BaseModel
{
public string Creator { get; set; }
public string Name { get; set; }
public int SO { get; set; }
public int SOD { get; set; }
public int SAP { get; set; }
public int BA { get; set; }
public int Invoice { get; set; }
public int EPD { get; set; }
public double DP { get; set; }
}
i will try to explain more detail as much i can, so i want to get all of my property from Jtabel class to my ProcessCollection where inside the method all of Jtable property will test each element that's contain searchText.
I am working with a legacy database, within this database, the data get assigned the maximum length of the column. if the string data is shorter, it will automaticly fill in whitespaces at the end.
What i'm trying to do is trim all these ending whitespaces with every query i do.
i figured one of the better ways would be making an extension method for dapper query using reflection.
But i can't seem to get it to work.
parent entity:
public class Person: BaseEntity
{
public Identification Identification { get; set; }
public Address Address { get; set; }
public Contact Contact { get; set; }
public Family Family { get; set; }
public ICollection<Payment> Payments { get; set; }
}
example of child entity:
public class Address: BaseEntity
{
public string Street { get; set; }
public int Number { get; set; }
public string BoxNumber { get; set; }
public int ZipCode { get; set; }
public string City { get; set; }
public string Country { get; set; }
}
Now i do my join query like this:
var result = _db.QueryTrim<dynamic>(sql, new { userid = id })
.Select(p => new Person()
{
Identification = IdentificationMapper.MapToIdentificationEntity(p),
Address = AddressMapper.MapToAddressEntity(p)}).First();
i am trying to implement something like this but i can't get it to work with query
public static class DapperExtensions {
public static IEnumerable<T> Query<T>(this IDbConnection cnn, string sql, object param = null, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = null, CommandType? commandType = null) {
var dapperResult = SqlMapper.Query<T>(cnn, sql, param, transaction, buffered, commandTimeout, commandType);
var result = TrimStrings(dapperResult.ToList());
return result;
}
static IEnumerable<T> TrimStrings<T>(IList<T> objects) {
//todo: create an Attribute that can designate that a property shouldn't be trimmed if we need it
var publicInstanceStringProperties = typeof (T).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.PropertyType == typeof (string) && x.CanRead && x.CanWrite);
foreach (var prop in publicInstanceStringProperties) {
foreach (var obj in objects) {
var value = (string) prop.GetValue(obj);
var trimmedValue = value.SafeTrim();
prop.SetValue(obj, trimmedValue);
}
}
return objects;
}
static string SafeTrim(this string source) {
if (source == null) {
return null;
}
return source.Trim();
}
}
In the end i want to trim all string that come out of the database. At the moment i'm tunnelvisioning on the dapperextension, but if there is any better way. Please let me know.
If you are using Dapper version 1.50.2 you can do it in a simpler way.
Create a TypeHandler like the one bellow:
public class TrimmedStringHandler : SqlMapper.TypeHandler<string>
{
public override string Parse(object value)
{
string result = (value as string)?.Trim();
return result;
}
public override void SetValue(IDbDataParameter parameter, string value)
{
parameter.Value = value;
}
}
On the program initialization you must call the AddTypeHandler method of the SqlMapper class like bellow:
SqlMapper.AddTypeHandler(new TrimmedStringHandler());
If you do this, every string that come from database will be trimmed.
My Service returns a JSON like below, I want to Parse this and bind category_name to my list, how to do this in C#, UWP apps.
Iam trying to Deserialize this in this way
var content_resp = await response.Content.ReadAsStringAsync();
content_resp = "{ \"root\": " + content_resp.Trim().TrimStart('{').TrimEnd('}') + " }";
var xmlProducts = JsonConvert.DeserializeXNode(content_resp);
var xmlProductNodes = xmlProducts.DescendantNodes();
foreach (XElement xmlProduct in xmlProductNodes)
{
Places pl = new Places();
var node = xmlProducts.Element("category_parent").Value;
pl.category_name = xmlProducts.Element("category_name").Value;
}
}
catch (Exception ex)
{
//throw or return an appropriate response/exception
}
when the debuger comes at this line
var xmlProducts = JsonConvert.DeserializeXNode(content_resp);
it is giving an error like
"This operation would create an incorrectly structured document."
The easiest way that I have found is to use Newtonsoft.Json
Firstly you should create class with data equal to your JSON:
public class AppsData
{
private string _category_id;
private string _category_name;
public string category_id
{
get { return _category_id; }
set { _category_id = value; }
}
public string category_name
{
get { return _category_name; }
set { _category_name = value; }
}
}
Then you can create some helper class:
static class JSONhelper
{
public static IList<T> DeserializeToList<T>(string jsonString)
{
var array = Newtonsoft.Json.Linq.JArray.Parse(jsonString);
IList<T> objectsList = new List<T>();
foreach (var item in array)
{
try
{
objectsList.Add(item.ToObject<T>());
}
catch { }
}
return objectsList;
}
}
Then create instance of List
IList<AppsData> appsdata;
and try to deserialize:
appsdata = JSONhelper.DeserializeToList<AppsData>(stringWithJSON);
If there is no reason to deserialize to XML you can do the following:
Create the class to parse
public class Product
{
public string category_id { get; set; }
[JsonProperty("0")]
public string Zero { get; set; }
public string category_name { get; set; }
[JsonProperty("1")]
public string One { get; set; }
public string category_details { get; set; }
[JsonProperty("2")]
public string Two { get; set; }
public string category_link { get; set; }
[JsonProperty("3")]
public string Three { get; set; }
}
And then
var products = JsonConvert.DeserializeObject<IList<Product>>(your json);
I have a model structure as illustrate below.
public class GuideLineSectionsViewModel
{
public GuideLineSectionsViewModel()
{
SectionsSet = new List<SectionViewModel>();
}
public string Title { get; set; }
public List<SectionViewModel> SectionsSet { get; set; }
}
public class SectionViewModel
{
public SectionViewModel()
{
SectionsSet = new List<SectionViewModel>();
QuestionsSet = new List<QuestionViewModel>();
ProblemsSet = new List<ProblemViewModel>();
GoalsSet = new List<GoalViewModel>();
BarriersSet = new List<BarriersViewModel>();
QuestionReferencesSet = new List<QuestionReferenceViewModel>();
}
public string Heading { get; set; }
public List<SectionViewModel> SectionsSet { get; set; }
public List<QuestionViewModel> QuestionsSet { get; set; }
public List<ProblemViewModel> ProblemsSet { get; set; }
public List<GoalViewModel> GoalsSet { get; set; }
public List<BarriersViewModel> BarriersSet { get; set; }
public List<QuestionReferenceViewModel> QuestionReferencesSet { get; set; }
}
public class ProblemViewModel
{
public string Text { get; set; }
public bool Identified { get; set; }
public List<GoalViewModel> GoalsSet { get; set; }
public List<QuestionReferenceViewModel> QuestionReferencesSet { get; set; }
}
Now Based on the condition I need to update the every list value of the ProblemViewModel using linq.Below is the condition
public GuideLineSectionsViewModel FindGuidelineType(GuideLineSectionsViewModel guidelineSectionModel)
{
//GuideLineSectionsViewModel result = new GuideLineSectionsViewModel();
string title = guidelineSectionModel.Title;
int count = Regex.Matches(title, "Low Intensity").Count;
if (count > 0)
{
}
return guidelineSectionModel;
}
The guidelineSectionModel.Title will contain the text as "some value : Low Intensity". So i used the regx to filter the text. Is there other way i can directly check the condition in linq. and update the model model.
I want to update list value of ProblemViewModelmodel property value public bool Identified to "true"
Currently it contain only False value.
Please can anyone help me to solve the issue.
Have a look at following method. I could not put LINQ but I think this answer can solve your purpose. Again Some classes structure are missing in your question so you may need to put that in following method.
GuideLineSectionsViewModel FindGuidelineType(GuideLineSectionsViewModel guidelineSectionModel)
{
//GuideLineSectionsViewModel result = new GuideLineSectionsViewModel();
string title = guidelineSectionModel.Title;
int count = Regex.Matches(title, "Low Intensity").Count;
if (count > 0)
{
foreach(SectionViewModel svm in guidelineSectionModel.SectionsSet)
{
foreach(ProblemViewModel pvm in svm.ProblemsSet)
{
pvm.Identified = true;
}
}
}
return guidelineSectionModel;
}
If you prefer LINQ:
if(guideLine.Title.Contains("Low Intensity"))
{
guideLine.SectionsSet.ForEach(s => s.ProblemsSet.ForEach(ps => ps.Identified = true));
}
Note: please read this answer https://stackoverflow.com/a/2962689/1525637 due to possible performance problems with the Regex.Matches, you should use String.Contains instead.
Trying to get the result from a webservice call to return a Model. I eep getting the error:
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'CI.Models.Schedule' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
public Schedule getCourseSchedule()
{
var obj = new
{
States = new[] { new { State = "MX" } },
Zip = "",
Miles = "",
PaginationStart = 1,
PaginationLimit = 3
};
using (var client = new WebClient())
{
client.Headers[HttpRequestHeader.ContentType] = "apoplication/json";
var url = "http://192.168.1.198:15014/ShoppingCart2/CourseSchedule";
var json = JsonConvert.SerializeObject(obj);
byte[] data = Encoding.UTF8.GetBytes(json);
byte[] result = client.UploadData(url, data);
string returnjson = Encoding.UTF8.GetString(result);
Schedule sched = JsonConvert.DeserializeObject<Schedule>(returnjson);
return sched;
}
}
Schedule Model:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Globalization;
namespace CI.Models
{
public class Schedule
{
public IEnumerable<Course> Courses { get; set; }
}
public class Course
{
/*
JSON Data returned from web service:
{
"ProgramGroup":"MR",
"ProgramCode":"RM",
"EventCode":"20160901MXMR",
"FormalDate":"September 1-2, 2016",
"StartDate":"2016\/09\/01",
"Price":5,
"LocName":"WB Hotel",
"LocAddress":"Av. Speedy Gonzales 220",
"LocCity":"Monterrey",
"LocState":"MX",
"LocZipCode":null,
"LicenseeURL":null,
"AgendaURL":"NA",
"SeatsAreAvailable":"2",
"GeneralInfoHTML":"General Info goes here.",
"GateKeeperHTML":null,
"EventType":"SS",
"TotalCourses":3
}
*/
public string ProgramGroup { get; set; }
public string ProgramCode { get; set; }
public string EventCode { get; set; }
public string FormalDate { get { return FormalDate; } set { FormalDate = convertFormalDateToSpanish(value); } }
public string StartDate { get; set; }
public double Price { get; set; }
public string LocName { get; set; }
public string LocAddress { get; set; }
public string LocCity { get ; set; }
public string LocState { get; set; }
public string LocZipCode { get; set; }
public string LicenseeURL { get; set; }
public string AgendaURL { get { return AgendaURL; } set { AgendaURL = buildAgendaLink(value); } }
public string SeatsAreAvailable { get; set; }
public string GeneralInfoHTML { get; set; }
public string GateKeeperHTML { get; set; }
public string EventType { get; set; }
public int TotalCourses { get; set; }
public string convertFormalDateToSpanish(string val)
{
DateTime TheDate = DateTime.Parse(StartDate);
string[] FormalDate = val.Split(" ".ToCharArray());
CultureInfo ci = new CultureInfo("es-ES");
string _Date = FormalDate[1].Replace("-", " al ").Replace(",", "");
string _Month = ci.TextInfo.ToTitleCase(TheDate.ToString("MMMM", ci));
val = string.Concat(_Date, " ", _Month);
return val;
}
private string buildAgendaLink(string val)
{
if (val.Trim() != "")
{
val = string.Concat("Agenda");
}
else
{
val = "Agenda";
}
return val;
}
}
}
Your server returns an array. Just try
Course[] courses = JsonConvert.DeserializeObject<Course[]>(returnjson);
Note that this is not an answer to your original problem, but I added it like an answer in order to explain my comment above with some actual code.
First problem with your code is that FormalDate and AgendaUrl properties simply won't work. Accessing them will result in a StackOverflowException, because you basically defined them recursively.
A property is merely syntax sugar for two separate getter/setter methods, so by writing this:
public class Course
{
public string FormalDate
{
get { return FormalDate; }
}
}
You are basically writing this:
public class Course
{
public string GetFormalDate()
{
// recursive call, with no terminating condition,
// will infinitely call itself until there is no
// more stack to store context data (and CLR
// will then throw an exception)
return GetFormalDate();
}
}
To fix that, you need to add an actual backing field, e.g.:
public class Course
{
private string _formalDate; // <-- this is a backing field;
// and this property uses the backing field to read/store data
public string FormalDate
{
get { return _formalDate; }
set { _formalDate = convertFormalDateToSpanish(value); }
}
}
Additionally, it's unusual for a property getter to return a different value than the one set through a setter. In other words, I would never expect this from a class:
var course = new Course();
course.StartDate = "2016/09/01";
course.FormalDate = "September 1-2, 2016";
Console.WriteLine(course.FormalDate); // prints "1 al 2 Septiembre" ?
I would rather move this functionality into a different class, or at least create different properties which return these values:
public class CourseInfo
{
// this is now a "dumb" auto-implemented property
// (no need for a backing field anymore)
public string FormalDate { get; set; }
// this read-only property returns the converted value
public string LocalizedFormalDate
{
get
{
return convertFormalDateToSpanish(FormalDate);
}
}
}