How to implement a Query with IQueryable in SilverLight - linq

I have two tables customers, pay and want to implement a gridview in silverlight with the outcome of the relationship of these two tables, the query is as follows
SELECT Pa.Tipo_Pagare, Pa.Pagare, Pa.Rut, Cli.Nombre
FROM Cred_Crexsa.dbo.Pagare AS Pa INNER JOIN
Cred_Crexsa.dbo.Clientes AS Cli ON Pa.Rut = Cli.Rut
WHERE (Pa.Nulo <> 0) AND (Pa.Extraviado <> 0)
Thank you very much
Leonardo Moreno Flores

Silverlight doesn't have any DB libraries. You'll need to write a webservice. Either reconstruct the query on the web server using your ORM of choice (many of them now support LINQ features, if you prefer) or have it call a stored procedure based on the SQL you posted.

Related

Optimizing Hibernate layer

After moving the logic from a legacy application (SQL/coldfushion) to Spring Rest with Hibernate, we have experienced a slowness in the application. The main reason is with Hibernate we noticed many queries are generated which we used to do with one single query in the legacy application (two pages long query).
Write now, I'm looking at selecting proper fetch strategies and try to optimize code. Could you please give me any other areas that I need to investigate to optimize the Hibernate layer or any other sujjestions?
Try to use DTO not entities(you can load DTO directly from the database)
Review the loading strategy (Eager or Lazy)
Try to use Native Queries more
Try to use more parameters to restrict the result set
You also can leverage some caching technique (cache all static data)
Try to implement hashCode and equals for each entity
If you use, HQL queries, then add the 'join fetch', It avoids the n+1 query problems. For more information on join fetch
e.g:
select a from Model a
inner join fetch a.b as b
Add 'indexes' for columns which are using in where condition.
e.g: Add index for the column 'name' which is used in where condition.
select a from Model a where a.name ='x'
Follow the below links:
http://www.thoughts-on-java.org/tips-to-boost-your-hibernate-performance/
https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/performance.html

How to join more than two tables using Lightswitch LINQ query

I am new to lightswitch as well as linq, i am using LS 2015. I want to know how to display the data of one table in other table's screen.
For example I have job vacancies, where I want to show candidate professional information who has applied for job. These two tables are related through Candidate personal ID, means a foreign key between candidate personal table and professional table.
The relation is as follows
JobVacancies->CandidatePersonalInfor->candidateProfessionalInfo. They are many to one related.
I have written a linq query to fetch data, where i have included some other tables also, my code is as follows
Any help is appreciated.
Have you defined the relationships you describe between the entities? If so, you'll have the ability to add and drop the details you require onto the form in the designer. The entities and their properties will be in the left pane of the screen designer.
If the join operation is more complex, you can use a WCF RIA Service to create a composite entity, as described in http://lightswitchhelpwebsite.com/Blog/tabid/61/EntryId/2226/Creating-a-WCF-RIA-Service-for-Visual-Studio-2013.aspx

LINQ to CRM - OR in Where clause

I am trying to bring some data from Dynamics CRM 2011 using LINQ. The goal is to bring all Contact records that have changes since certain date OR have a child entity (PERC files) changed since that same date. The query looks like that:
// Bring all students who have changes (at Contact entity) after specific date
// momentInTime or the status of any of their perc files has been changed since
// that date
var students = (from c in ContactSet
join pl in cga_portallogonSet on c.Id equals pl.cga_ContactId.Id
join ef in cga_percfileSet on c.Id equals ef.cga_StudentId.Id
where
(pl.cga_PortalLogonRole.Value == 284970000) // student
where
(c.ModifiedOn >= momentInTime || c.CreatedOn > momentInTime)
||
(ef.cga_statuschangedate >= momentInTime)
select c.cga_StudentNumber).Distinct().ToList();
This produces the following error message:
'Contact' entity doesn't contain attribute with Name = 'cga_statuschangedate'.
I cannot figure out how to do OR on two different entities. The MSDN says you need WHERE clause for each entity:
where Clause
In order to filter the result set, where clauses can be added against one or more of the >entities. Each where clause may only contain conditions against an individual entity type. >A composite condition involving multiple entities is not valid. Instead, each entity >should be filtered in separate where clauses.
http://msdn.microsoft.com/en-us/library/ff681565.aspx
Is there another way of achieving what I need?
Unfortunately you cannot achieve what you want in a single linq statement beacuse the liunq provider they use boils down to fetchXML, and fetchXML does not support the scenario you are using.
More detail... Fetch gives you condition's inside of entity's or link-entity's. These condition elements cannot have attributes in them from other linked entities, only the direct parent entity or link-entity. Here is one of many microsoft forum posts referencing this limitation of fetchXML
Probably not the answer you were looking for, eh? As an ugly alternative you you can run two separate queries and filter in memory (as damaging as that may be to performance). Or better yet if you are an on-premise deployment you can write some sql against the filtered views. Good luck.

When to use JOIN and when not to in LINQ to entities

I am new to Linq and I have seen that if there are multiple entities, some use the multiple FROM syntax like this:
from h in db.Hubs
from ch in h.CityHubs where ch.Cities.CityID == 1
select
and some use the explicity join syntax.
from h in db.Hubs
join ch in da.CityHubs on h.CityId equals ch.CityId
select
If I am using Linq to entities, which one should I use? If I were to use Linq to objects, which one should I use?
As a rule, in Entity Framework, if you have a proper model and properly set up navigation properties for foreign keys, you should almost never use join - instead you access your navigational property directly and EF will generate the necessary join in the SQL code for you. I recommend taking a look at #Craig Stuntz's blogpost regarding this issue.
Regarding Linq-to-objects, however, it depends on the particular query you are writing.

Collection Exists Criteria in WCF Data Services

I'm trying to ask my OData service:
"Give me all the Products that do not have a Category"
with the Products and Category have a m2m relationship.
I've tried:
from p in Products
where p.Categories == null
select p
and
from p in Products
where !p.Categories.Any()
select p
and
from p in Products
where p.Categories.Count == 0
select p
but all of these give me not supported exceptions.
I'm not looking for alternatives or options. Please don't answer with other options.
My experience with WCF Data Services is that the client subset of LINQ to rest is lacking.
My biased option would be to just move it to server side where you have access to the full implementation of LINQ to entites? (or whatever you are using to implement your Data Service).
[WebGet]
public IQueryable<Products> GetProductsWithoutCategories(){
/*start psudo code
from p in Products
where p.Categories.Count == 0
select p
*/
}
This is not supported:
http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataservices/thread/b505d630-c808-4bde-b08e-3ce1dd17f621/
The OData URL query language currently
doesn't support this type of query. As
a result the client's LINQ processor
doesn't support it either. If you
think it's valuable to add such
functionality please use our connect
site to suggest the feature, it makes
our planning job easier next time
around.
https://connect.microsoft.com/dataplatform/content/content.aspx?ContentID=15540&wa=wsignin1.0
As a workaround you could probably use
service operation. Define a service
operation which returns IQueryable (so
that you can compose some more query
operators on the result from the
client) and use the server side
provider to issues the query above.
My solution to this problem was to $expand the related field in the query, and then test if that field was empty or not...
JArray _array = (JArray)main_table_object["some_related_field"];
if (_array.Count > 0)
continue;
.. this is in the context of queries from Windows 7 phone to a WCF service using JSON as the message format.

Resources