How to deal to have a good performance with complex data query like this :
In my Data access layer :
public IEnumerable<Serie> Search(SearchCriteria searchCriteria)
{
//Operation to have my predicate...
return ListAll().Where(predicate);
}
In my SerieFicheViewModel Adapter in order to adapt my entity to a ViewModel :
public static List<SerieFicheViewModel> ToViewModel(int utilisateurProfilId, IEnumerable<Serie> series, int utilisateurId, List<string> roles)
{
IEnumerable<SerieFicheViewModel> seriesFiches =
from s in series
select new SerieFicheViewModel
{
serie = SerieAdapter.ToViewModel(s, utilisateurId, SerieAdapter.TypeReponseEnum.Small),
serieUtilisateur = SerieUtilisateurAdapter.ToViewModel(s, utilisateurId, roles)
};
return seriesFiches.ToList();
}
My SerieAdapter :
public static SerieViewModel ToViewModel(Serie serie, int utilisateurId, TypeReponseEnum typeReponse)
{
SaisonBusiness _saisonBusiness = (SaisonBusiness)UnityHelper.BusinessResolve<Saison>();
EpisodeBusiness _episodeBusiness = (EpisodeBusiness)UnityHelper.BusinessResolve<Episode>();
int nombreSaisonsValides = _saisonBusiness.GetNombreSaisonsValides(serie.serie_id);
SerieViewModel SerieViewModel = new SerieViewModel
{
id = serie.serie_id,
dateAjout = serie.serie_dateajout.ToString("dd/MM/yyyy HH:mm"),
nomVf = serie.Prestation.prestation_nom,
nomOriginal = serie.Prestation.prestation_nom2,
nom = serie.Prestation.prestation_nom,
imageThumbUrl = !string.IsNullOrEmpty(serie.Prestation.prestation_image_thumb) ? ConfigurationManager.AppSettings["AddictLive_BaseUrl"] + serie.Prestation.prestation_image_thumb.Replace("~", "").Substring(1) : string.Empty,
imageUrl = !string.IsNullOrEmpty(serie.Prestation.prestation_image) ? ConfigurationManager.AppSettings["AddictLive_BaseUrl"] + serie.Prestation.prestation_image.Replace("~", "").Substring(1) : string.Empty,
noteMoyenne = serie.Prestation.PrestationNotes.Count() > 0 ? serie.Prestation.PrestationNotes.Select(pn => pn.note).Average() : 0,
synopsis = serie.Prestation.prestation_description,
format = serie.serie_format,
nombreDeNotes = serie.Prestation.PrestationNotes.Count(),
remerciement = serie.UserAuth != null ? serie.UserAuth.DisplayName : string.Empty,
statusProduction = serie.StatusProduction != null ? serie.StatusProduction.statusproduction_nom : string.Empty,
duree = RenseignerDuree(serie),
nombreSaisons = nombreSaisonsValides,
nombreEpisodes = _episodeBusiness.GetNombreEpisodesValides(serie.serie_id),
videoPrincipale = serie.serie_videoprincipale
};
SerieViewModel.ficheUrl = UrlTool.GetSerieFicheUrl(serie.serie_id, serie.Prestation.prestation_nom);
PrestationNoteBusiness _prestationNoteBusiness = (PrestationNoteBusiness)UnityHelper.BusinessResolve<PrestationNote>();
SerieViewModel.noteMoyenneEpisodes = _prestationNoteBusiness.GetMoyenneDeTousLesEpisodes(serie.serie_id);
//Dispo tout le temps pour afficher un lien vers la premiere saison une fois qu'on a mis une série comme vue
Saison saison = nombreSaisonsValides > 0 ? _saisonBusiness.GetPremiereSaisonValide(serie.serie_id) : null;
if(saison != null)
SerieViewModel.saisonUrl = UrlTool.GetSaisonUrl(saison.saison_id, saison.saison_numero.ToString(), serie.Prestation.prestation_nom);
return SerieViewModel;
}
I do not put all the code to try not to pollute the request but you can see my adapters are complex and I don't know how tu customize them.
In the above example I use lazy loading and more than 215 queries are executed in my database
If I try using eager loading like recommanded in the Entity Framework Profiler software :
public IEnumerable<Serie> Search(SearchCriteria searchCriteria)
{
//Operation to have my predicate...
return ListAll().Include(s => s.Prestation.PrestationNotes)
.Include(s => s.UtilisateurSerieEtats)
.Include(s => s.UtilisateurSerieSuivies)
.Include(s => s.Saisons.Select(sai => sai.Episodes))
.Where(predicate);
}
Entity framework Profiler said this time there is too much joins...
obviously my request is too big and too much data recovered but how to cut my code and improve performance ? I have this situation with almost all my adapters and I need your help.
Thank you in advance to all who take the time to read and help me
One way to improve your performance would be to create a view (or several views). Your EF query could then query a much simpler structure and pull all the data back i one call.
Depending on the version of SQL Server you have available you could use Indexed Views, this can have a dramatic effect on performance.
Related
Using the Microsoft.CrmSdk assembly to generate entities in Dynamics 365 for Customer Engagement (version 9), I found out that the method GetEntityMetadata from CrmServiceClient does not get the most uptodate information from entities.
Here the code to show you:
using (var svc = new CrmServiceClient(strConn))
{
EntityMetadata em = svc.GetEntityMetadata(PREFIX + TABLE_NAME_D, EntityFilters.Attributes);
if (em == null)
{
Console.WriteLine($"Create entity [{PREFIX + TABLE_NAME_D}]");
CreateEntityRequest createRequest = new CreateEntityRequest
{
Entity = new EntityMetadata
{
SchemaName = PREFIX + TABLE_NAME_D,
LogicalName = PREFIX + TABLE_NAME_D,
DisplayName = new Label(TABLE_LABEL, 1036),
DisplayCollectionName = new Label(TABLE_LABEL_P, 1036),
OwnershipType = OwnershipTypes.UserOwned,
},
PrimaryAttribute = new StringAttributeMetadata
{
SchemaName = PREFIX + "name",
MaxLength = 30,
FormatName = StringFormatName.Text,
DisplayName = new Label("Residence", 1036),
}
};
CreateEntityResponse resp = (CreateEntityResponse)svc.Execute(createRequest);
em = svc.GetEntityMetadata(PREFIX + TABLE_NAME_D, EntityFilters.All);
// At this point, em is null!!!
}
}
After the createResponse is received, the entity is well created in Dynamics, but still the GetEntityMetadata called just after is still null. If I wait a few seconds and make another call, the response is now correct. But that's horrible!
Is there any way to "force" the refresh of the response?
Thanks.
Ok I found it! It's linked to a caching mechanism.
One must use the function ResetLocalMetadataCache to clean the cache, but there seems to be an issue with this function.
It will only works by passing the entity name in parameter (if you call it without parameter, it is supposed to clean the entire cache but that does not work for me).
EntityMetadata em = svc.GetEntityMetadata(TABLE_NAME_D, EntityFilters.All); // Request sent
em = svc.GetEntityMetadata(TABLE_NAME_D, EntityFilters.All); // Cache used
svc.ResetLocalMetadataCache(); // No effect?!
em = svc.GetEntityMetadata(TABLE_NAME_D, EntityFilters.All); // Cache used
em = svc.GetEntityMetadata(TABLE_NAME_D, EntityFilters.All); // Cache used
svc.ResetLocalMetadataCache(TABLE_NAME_D); // Cache cleaned for this entity
em = svc.GetEntityMetadata(TABLE_NAME_D, EntityFilters.All); // Request sent!
I am trying to fill a viewmodel(List) from Datatable.
Customer and WorkingTime
WorkingTime is a child List of Customer. There are several entries for a Customer.
Using Adapter.Fill Meathod
DataTable dtCust=GetCustomersInTable(Uid);
DataTable dtWorking=GetAllCustWorkTimeInTable(Uid);
var Customers = (from C in dtCust.AsEnumerable()
select new CustomerViewModel
{
CustomerId = (long)C["CustomerId"],
Address1 = DBNull.Value == C["Name"] ? "" : Convert.ToString(C["Name"]),
Address2 = DBNull.Value == C["Address"] ? "" : Convert.ToString(C["Address"]),
City = DBNull.Value == C["City"] ? "" : Convert.ToString(C["City"]),
WorkingTime = GetCustWorkingTime(dtWorking, Convert.ToInt64(C["CustomerId"])),
}}).ToList();
private List<CustomerWorkingTime> GetCustWorkingTime(DataTable dtWorking, long CustomerId)
{
var CustomerWorkingTimes = (from C in dtWorking.AsEnumerable()
where Convert.ToInt64(C["CustomerId"]) == CustomerId
select new CustomerWorkingTime
{
AfterNoonFrom = Convert.ToDateTime(C["AfterNoonFrom"]),
AfterNoonUntil = Convert.ToDateTime(C["AfterNoonUntil"])
}).ToList();
return result;
}
I need to take whole data. It used not for displaying in UI.
This meathod is taking too much time to fill data. especially CustomerWorkingtime Filling.
Please suggest a better method to fill data.
Pagination is not possible here.
Existing setup is in EntityFramework and performance is poor.
Yu can try playing around with the TPL Parallel.ForEach, I think in your case this might give you a boost.
for example:
var Customers = Parallel.ForEach(dtCust.AsEnumerable(), c =>
select new CustomerViewModel
{
CustomerId = (long)C["CustomerId"],
Address1 = DBNull.Value == C["Name"] ? "" : Convert.ToString(C["Name"]),
Address2 = DBNull.Value == C["Address"] ? "" : Convert.ToString(C["Address"]),
City = DBNull.Value == C["City"] ? "" : Convert.ToString(C["City"]),
WorkingTime = GetCustWorkingTime(dtWorking, Convert.ToInt64(C["CustomerId"])),
}}).ToList();
Or in your "GetCustWorkingTime":
var result= dtWorking
.AsEnumerable()
.AsParallel()
.Where(c => Convert.ToInt64(C["CustomerId"]) == CustomerId)
.Select(c= > new CustomerWorkingTime
{
AfterNoonFrom = Convert.ToDateTime(C["AfterNoonFrom"]),
AfterNoonUntil = Convert.ToDateTime(C["AfterNoonUntil"])
}).ToList();
you should try them separately, and together, to see which one gives you the best performance (depends on the size of the tables).
Note: using both together might cause problems since in "GetCustWorkingTime" you'll end up iterating over the same table by multiple tasks in the same time.
I have an IEnumerable collection.
Using LINQ, I am populating the collection from a web service response.
Below is the sample I am using.
lookupData = from data in content["data"].Children()
select new LookupData
{
LookupKey = (data["data"]["key"]).ToString(),
LookupValue = (string)data["data"]["name"]
};
I will be using the same code for a lot of similar responses which will return a key and value.
Now, I got a scenario when I needed an additional field from the service response for few of the responses(not for all). So, I created an "Optional" property in "LookUpData" class and used as below:
lookupData = from data in content["data"].Children()
select new LookupData
{
LookupKey = (data["data"]["key"]).ToString(),
LookupValue = (string)data["data"]["name"],
Optional = referenceConfig.Optional != null
? (data["data"]["optional"]).ToString()
: String.Empty
};
The null check here is a performance issue. I do not want to use the below since I have other conditions and all together it will become a very big if else loop.
if(referenceConfig.Optional != null){
lookupData = from data in content["data"].Children()
select new LookupData
{
LookupKey = (data["data"]["key"]).ToString(),
LookupValue = (string)data["data"]["name"],
Optional = (data["data"]["optional"]).ToString()
};
}
else{
lookupData = from data in content["data"].Children()
select new LookupData
{
LookupKey = (data["data"]["key"]).ToString(),
LookupValue = (string)data["data"]["name"]
};
}
But I have at least 10 web server response with lots of data in each.
If the value of referenceConfig.Optional is available at compile time you can do
#if OPTIONAL
...
#else
...
If not - you can implement the Null Object Pattern i.e. have all of your ["data"][...] properties always return a value(for instance string.Empty if the type is string) so you won't have check explicitly in the code.
Using Linq commands and Linq To SQL datacontext, Im trying to instance an Entity called "Produccion" from my datacontext in this way:
Demo.View.Data.PRODUCCION pocoProduccion =
(
from m in db.MEDICOXPROMOTORs
join a in db.ATENCIONs on m.cmp equals a.cmp
join e in db.EXAMENXATENCIONs on a.numeroatencion equals e.numeroatencion
join c in db.CITAs on e.numerocita equals c.numerocita
where e.codigo == codigoExamenxAtencion
select new Demo.View.Data.PRODUCCION
{
cmp = a.cmp,
bonificacion = comi,
valorventa = precioEstudio,
codigoestudio = lblCodigoEstudio.Content.ToString(),
codigopaciente = Convert.ToInt32(lblCodigoPaciente.Content.ToString()),
codigoproduccion = Convert.ToInt32(lblNroInforme.Content.ToString()),
codigopromotor = m.codigopromotor,
fecha = Convert.ToDateTime(DateTime.Today.ToShortDateString()),
numeroinforme = Convert.ToInt32(lblNroInforme.Content.ToString()),
revisado = false,
codigozona = (c.codigozona.Value == null ? Convert.ToInt32(c.codigozona) : 0),
codigoclinica = Convert.ToInt32(c.codigoclinica),
codigoclase = e.codigoclase,
}
).FirstOrDefault();
While executing the above code, I'm getting the following error that the stack trace is included:
System.NotSupportedException was caught
Message="The explicit construction of the entity type 'Demo.View.Data.PRODUCCION' in a query is not allowed."
Source="System.Data.Linq"
StackTrace:
en System.Data.Linq.SqlClient.QueryConverter.VisitMemberInit(MemberInitExpression init)
en System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node)
en System.Data.Linq.SqlClient.QueryConverter.Visit(Expression node)
en System.Data.Linq.SqlClient.QueryConverter.VisitSelect(Expression sequence, LambdaExpression selector)
en System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(MethodCallExpression mc)
en System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(MethodCallExpression mc)
en System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node)
en System.Data.Linq.SqlClient.QueryConverter.Visit(Expression node)
en System.Data.Linq.SqlClient.QueryConverter.VisitFirst(Expression sequence, LambdaExpression lambda, Boolean isFirst)
en System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(MethodCallExpression mc)
en System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(MethodCallExpression mc)
en System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node)
en System.Data.Linq.SqlClient.QueryConverter.ConvertOuter(Expression node)
en System.Data.Linq.SqlClient.SqlProvider.BuildQuery(Expression query, SqlNodeAnnotations annotations)
en System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)
en System.Data.Linq.DataQuery`1.System.Linq.IQueryProvider.Execute[S](Expression expression)
en System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source)
en Demo.View.InformeMedico.realizarProduccionInforme(Int32 codigoExamenxAtencion, Double precioEstudio, Int32 comi) en D:\cs_InformeMedico\app\InformeMedico.xaml.cs:línea 602
en Demo.View.InformeMedico.UpdateEstadoEstudio(Int32 codigo, Char state) en D:\cs_InformeMedico\app\InformeMedico.xaml.cs:línea 591
en Demo.View.InformeMedico.btnGuardar_Click(Object sender, RoutedEventArgs e) en D:\cs_InformeMedico\app\InformeMedico.xaml.cs:línea 683
InnerException:
Is that now allowed in LINQ2SQL?
Entities can be created outside of queries and inserted into the data store using a DataContext. You can then retrieve them using queries. However, you can't create entities as part of a query.
I am finding this limitation to be very annoying, and going against the common trend of not using SELECT * in queries.
Still with c# anonymous types there is a workaround, by fetching the objects into an anonymous type, and then copy it over into the correct type.
For example:
var q = from emp in employees where emp.ID !=0
select new {Name = emp.First + " " + emp.Last, EmployeeId = emp.ID }
var r = q.ToList();
List<User> users = new List<User>(r.Select(new User
{
Name = r.Name,
EmployeeId = r.EmployeeId
}));
And in the case when we deal with a single value (as in the situation described in the question) it is even easier, and we just need to copy directly the values:
var q = from emp in employees where emp.ID !=0
select new { Name = emp.First + " " + emp.Last, EmployeeId = emp.ID }
var r = q.FirstOrDefault();
User user = new User { Name = r.Name, EmployeeId = r.ID };
If the name of the properties match the database columns we can do it even simpler in the query, by doing select
var q = from emp in employees where emp.ID !=0
select new { emp.First, emp.Last, emp.ID }
One might go ahead and write a lambda expression that can copy automatically based on the property name, without needing to specify the values explictly.
Here's another workaround:
Make a class that derives from your LINQ to SQL class. I'm assuming that the L2S class that you want to return is Order:
internal class OrderView : Order { }
Now write the query this way:
var query = from o in db.Order
select new OrderView // instead of Order
{
OrderID = o.OrderID,
OrderDate = o.OrderDate,
// etc.
};
Cast the result back into Order, like this:
return query.Cast<Order>().ToList(); // or .FirstOrDefault()
(or use something more sensible, like BLToolkit / LINQ to DB)
Note: I haven't tested to see if tracking works or not; it works to retrieve data, which is what I needed.
I have found that if you do a .ToList() on the query before trying to contruct new objects it works
I just ran into the same issue.
I found a very easy solution.
var a = att as Attachment;
Func<Culture, AttachmentCulture> make =
c => new AttachmentCulture { Culture = c };
var culs = from c in dc.Cultures
let ac = c.AttachmentCultures.SingleOrDefault(
x => x.Attachment == a)
select ac == null ? make(c) : ac;
return culs;
I construct an anonymous type, use IEnumerable (which preserves deferred execution), and then re-consruct the datacontext object. Both Employee and Manager are datacontext objects:
var q = dc.Employees.Where(p => p.IsManager == 1)
.Select(p => new { Id = p.Id, Name = p.Name })
.AsEnumerable()
.Select(item => new Manager() { Id = item.Id, Name = item.Name });
Within the book "70-515 Web Applications Development with Microsoft .NET Framework 4 - Self paced training kit", page 638 has the following example to output results to a strongly typed object:
IEnumerable<User> users = from emp in employees where emp.ID !=0
select new User
{
Name = emp.First + " " + emp.Last,
EmployeeId = emp.ID
}
Mark Pecks advice appears to contradict this book - however, for me this example still displays the above error as well, leaving me somewhat confused. Is this linked to version differences? Any suggestions welcome.
I found another workaround for the problem that even lets you retain your result as IQueryale, so it doesn't actually execute the query until you want it to be executed (like it would with the ToList() method).
So linq doesn't allow you to create an entity as a part of query? You can shift that task to the database itself and create a function that will grab the data you want. After you import the function to your data context, you just need to set the result type to the one you want.
I found out about this when I had to write a piece of code that would produce a IQueryable<T> in which the items don't actually exist in the table containing T.
pbz posted a work around by creating a View class inherited from an entity class that you could be working with. I'm working with a dbml model of a table that has > 200 columns. When I try and return the whole table I get "Root Element missing" errors. I couldn't find anyone who wanted to deal with my particular issue so I was looking at rewriting my entire approach. Just creating a view class for the entitiy class worked in my case.
As pbz suggests : Create a view class that inherits from your entity class. For me this is tbCamp so :
internal class tbCampView : tbCamp
{
}
Then use the view class in your query :
using (var dc = ConnectionClass.Connect(Dev))
{
var camps = dc.tbCamps.Select(s => new tbCampView
{
active = s.active,
idCamp = s.idCamp,
campName = s.campName
});
SmartTableViewer(camps, dg1);
}
private void SmartTableViewer<T>(IEnumerable<T> allRecords)
{
// Build sorted rows back into new table
var table = new DataTable();
// Create columns based on type
if (allRecords is IEnumerable<tbCamp> tbCampRecords)
{
// Get the columns you want
table.Columns.Add("idCamp");
table.Columns.Add("campName");
foreach (var record in tbCampRecords)
{
// Make a new row
var r = table.NewRow();
// Add the contents to each column of the row
r["idCamp"] = record.idCamp;
r["campName"] = record.campName;
// Add the row to the table.
table.Rows.Add(r);
}
}
else
{
MessageBox.Show("Unhandled type. Add support for new data type in SmartTableViewer()");
return;
}
// Update table in grid
dg1.DataSource = table.DefaultView;
}
Here is what happens when you try and create an entity class object in the query.
I didn't want to have to use an anonymous type if I could help it because I wanted the type to be tbCamp. Since tbCampView is of type tbCamp the is operator works well. see Brian Hasden's answer Passing a generic List<> in C#
I'm surprised this is even an issue but with larger tables I run into this error so I thought I would just show it here :
When trying to read this table into memory I get the following error. There are < 2000 rows but the columns are > 200 for each. I don't know if that is an issue or not.
If I just want a few columns I need to create a custom class and handle that which isn't that big of a pain. With the approach pbz provided I don't have to worry about it.
Here is the entire project in case it helps someone.
public partial class Form1 : Form
{
private const bool Dev = true;
public Form1()
{
InitializeComponent();
}
private void btnGetAllCamps_Click(object sender, EventArgs e)
{
using (var dc = ConnectionClass.Connect(Dev))
{
IQueryable<tbCampView> camps = dc.tbCamps.Select(s => new tbCampView
{
// Project columns as needed.
active = s.active,
idCamp = s.idCamp,
campName = s.campName
});
// pass in as a
SmartTableViewer(camps);
}
}
private void SmartTableViewer<T>(IEnumerable<T> allRecords)
{
// Build sorted rows back into new table
var table = new DataTable();
// Create columns based on type
if (allRecords is IEnumerable<tbCamp> tbCampRecords)
{
// Get the columns you want
table.Columns.Add("idCamp");
table.Columns.Add("campName");
foreach (var record in tbCampRecords)
{
//var newRecord = record;
// Make a new row
var r = table.NewRow();
// Add the contents to each column of the row
r["idCamp"] = record.idCamp;
r["campName"] = record.campName;
// Add the row to the table.
table.Rows.Add(r);
}
}
else
{
MessageBox.Show("Unhandled type. Add support for new data type in SmartTableViewer()");
return;
}
// Update table in grid
dg1.DataSource = table.DefaultView;
}
internal class tbCampView : tbCamp
{
}
}
I have the following LINQ query
QueryResult<List<PersonDemographic>> members = new QueryResult<List<PersonDemographic>>();
var query = (from ms in this.Session.Query<MemberSummary>()
where ms.NameSearch.StartsWith(firstName.ToUpper())
&& ms.NameSearch2.StartsWith(lastName.ToUpper())
select new PersonDemographic
{
FirstName = ms.FirstName.ToProperCase(),
LastName = ms.LastName.ToProperCase(),
PersonId = ms.Id,
Address = new Address
{
Line1 = ms.AddressLine1.ToProperCase(),
Line2 = ms.AddressLine2.ToProperCase(),
City = ms.City.ToProperCase(),
State = ms.State,
Zipcode = ms.Zipcode,
},
PhoneNumber = new PhoneNumber
{
Number = string.IsNullOrWhiteSpace(ms.PhoneNumber) ? null : Regex.Replace(ms.PhoneNumber, #"(\d{3})(\d{3})(\d{4})", "$1-$2-$3")
}
});
if (this.Session.Transaction.IsActive)
{
members.Data = query.Distinct().Take(15).ToList();
}
else
{
using (var transaction = this.Session.BeginTransaction())
{
members.Data = query.Distinct().Take(15).ToList();
transaction.Commit();
}
}
The code is running under the transaction section. If I use it without a Distinct I have no problem. Adding the Distinct gives me an exception
{"Expression type 10005 is not supported by this SelectClauseVisitor."}
I can't find anything definitive. Can anyone help?
Thanks,
Paul
There are lots of things in that query that NH can't possibly know how to translate to SQL:
ToProperCase (that looks like an extension method of yours)
string.IsNullOrWhiteSpace (that's new in .NET 4, NH is compiled against 3.5)
Regex.Replace (that's just not possible to do with SQL, unless you have a DB that supports it and write a Dialect for it)