Converting sql to lambda expression or Linq [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
Hi I have following sql statement I want to convert it to lambda expression or linq
select
a.lid
,a.name
,a.notes
,lrh.e
,lrh.date
from rate a
left outer join lab_history lrh on(lrh.lab_id=a.lid)
Please let me know how to change this to lambda expression or linq. Thanks

You can use this query:
from a in db.rate
join lrh db.lab_history on a.lid equals lrh.lab_id
into lrhs
from lrh in lrhs.DefaultIfEmpty()
select new
{
lid = a.lid,
... another props
}

You can try
var labRateObj = (from a in ContextObj.rate
join lrh in ContextObj.lab_history on a.lid equals lrh.lab_id into labrate
from s in labrate.DefaultIfEmpty()
select new {
lid = a.lid,
name = a.name,
notes = a.notes,
e = lrh.e,
date = lrh.date
});

Related

LLBGen for dummies [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
Improve this question
I have an "opportunity" to work with LLBLGEN ORM, I have already spent two days on trying to query a JOIN command over several tables (without any success). Now at home I also trying to understand the logic of this ORM on smaller project (ie. Recruiter->JobOffer->Candidate).
And yet this piece of software is whooping my ass. So I went to the documentation and tried to get some knowledge from with, unfortunately without any success.
So I'm asking here, is there somewhere a tutorial for real dummies that easily explains the very fundamental usage of LLBLGEN :
where (ok, I've got this covered already)
join
multi join
Maybe someone has some code and db, that can share and illustrates this concepts done in friendly way.
Thanks!
Joins are expressed using a Relation object. If you want to retreive all Candidates of a specific Recruiter you could write something like this, asuming the tables have the correct Foreign Key relations
var list = new CandidateCollection();
var relationsToUse = new RelationCollection
{
JobOfferEntity.Relations.CandidateEntityUsingCandidateId,
RecruiterEntity.Relations.JobOfferEntityUsingJobOfferId
};
var filter = new PredicateExpression
{
new FieldCompareValuePredicate(RecruiterFields.Id, ComparisonOperator.Equal, recruiterId)
};
list.GetMulti(filter, relationsToUse);
There are some central concepts in LLBLGen which I'll explain with samples in code and equivalent Sql query.
Predicate, IPredicate and PredicateExpression: These are translated to Where clauses. You can think of PredicateExpression as a complex predicate that's made of several predicates joined with AND and OR.
Note: Treat the code snippets below as pseudo code as I don't have access to LLBLGen right now.
var pred = CustomerFields.Id == 5;
new DataAccessAdapter.FetchEntityCollection(coll, new RelationPredicateBucket(pred));
This will roughly translate to:
SELECT * FROM Customer WHERE Id = 5
You can combine several predicates using PredicateExpression:
var predEx = new PredicateExpression();
predEx.Add(CustomerFields.Id == 5);
predEx.AddWithOr(CustomerFields.Name == "X");
Is equivalent to:
SELECT * FROM Customer WHERE Id = 5 OR Name = 'X'
Relations: Relations represents the relations in your database. There's a Relations property in every Entity in generated code that contains every relation of that entity. For example if you have a Customer table that has a one-to-many relation with your Order table, the corresponding Entities will have a static Relations property that contains those relations:
CustomerEntity.Relations.OrderEntityUsingCustomerId;
OrderEntity.Relations.CustomerEntityUsingCustomerId;
You use these relations when you want to perform a join and return results based on the join. For example if you want to fetch all customers that have an order that's varlue is greater than 50000 you do this:
var pred = OrderFields.Value > 50000;
var rpb = new RelationPredicateBucket();
rpb.PredicateExpression.Add(pred);
rpb.Relations.Add(CustomerEntity.Relations.OrderEntityUsingCustomerId);//perform join
This will translate to:
SELECT C.* FROM Customer AS C JOIN Order AS O ON C.Id = O.CustomerId WHERE O.Value > 50000
For multiple joins you just add more relations, to get customer that have orders with values higher than 50000 that have an OrderDetail that's quantity is more than 1:
var pred = OrderFields.Value > 50000 & OrderDetailFields.Quantity > 1;
var rpb = new RelationPredicateBucket();
rpb.PredicateExpression.Add(pred);
rpb.Relations.Add(CustomerEntity.Relations.OrderEntityUsingCustomerId);//perform customer and order join
rpb.Relations.Add(OrderEntity.Relations.OrderDetailEntityUsingOrderId);//perform order and order detail join
Which produces this sql query:
SELECT C.* FROM Customer AS C JOIN Order AS O ON C.Id = O.CustomerId JOIN OrderDetail AS OD ON OD.OrderId = O.Id WHERE O.Value > 50000 AND OD.Quantity > 1

Linq & ASP.NET MVC: how to use "or" and "equals" in join statement [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
How to translate this sql query in a linq query, please?
USE [BiblioDB]
GO
SELECT *
FROM Exemplaire e
LEFT JOIN location l
ON e.ExemplaireId = l.ExemplaireId
INNER JOIN Retour r
ON l.LocationId is null or l.LocationId = r.LocationId
Go
I tried this linq query but the fourth line doesn't work.
BiblioDBContext biblioDBContext = new BiblioDBContext();
var query = from e in biblioDBContext.Exemplaires
join l in biblioDBContext.Locations on e.ExemplaireId equals l.LocationId
join r in biblioDBContext.Retours on l.LocationId == null || l.LocationId equals r.LocationId r.LocationId
select e;
Try this:
BiblioDBContext biblioDBContext = new BiblioDBContext();
var query = from e in biblioDBContext.Exemplaires
join l in biblioDBContext.Locations on e.ExemplaireId equals l.ExemplaireId into el
from l in el.DefaultIfEmpty()
from r in biblioDBContext.Retours
where (l.LocationId == null) || (l.LocationId.Equals(r.LocationId))
select new {e, l, r};
The issue is the missing DefaultIfEmpty() clause that is needed for Left Joins.

getting ORA-00933 while executing SQL query using ExecuteStoreQuery method [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am getting ORA-00933 error while executing SQL query using ExecuteStoreQuery method in C#.
Here is the code:
public IEnumerable<Administrator> FilterEmployees(string name)
{
Context db = new Context();
string query = "SELECT USER_ROLES.USER_ROLES_ID, EMPLOYEES.EMPLOYEE_ID, EMPLOYEES.EMPLOYEE_NAME, EMPLOYEES.SURNAME_1, EMPLOYEES.SURNAME_2, ROLES.NAME FROM USER_ROLES" +
"INNER JOIN EMPLOYEES ON USER_ROLES.USER_ID = EMPLOYEES.EMPLOYEE_ID" +
"INNER JOIN ROLES ON USER_ROLES.ROLE_ID = ROLES.ROLE_ID" +
"WHERE ROLES.IS_INTRANET_ONLY = 'N' AND EMPLOYEES.FULL_EMPLOYEE_NAME LIKE '%Yuriy%'";
List<Administrator> employees;
return db.ExecuteStoreQuery<Administrator>(query, "employees", System.Data.Objects.MergeOption.AppendOnly);
}
The query is fine(tested on Oracle SQL Developer)
You're concatenating strings, and there is no whitespace between the last word of one string and the beginning of the next. Leading to this query being executed : (formatting mine)
SELECT
USER_ROLES.USER_ROLES_ID,
EMPLOYEES.EMPLOYEE_ID,
EMPLOYEES.EMPLOYEE_NAME,
EMPLOYEES.SURNAME_1,
EMPLOYEES.SURNAME_2,
ROLES.NAME FROM USER_ROLESINNER JOIN EMPLOYEES ON USER_ROLES.USER_ID = EMPLOYEES.EMPLOYEE_IDINNER JOIN ROLES ON USER_ROLES.ROLE_ID = ROLES.ROLE_IDWHERE ROLES.IS_INTRANET_ONLY = 'N' AND EMPLOYEES.FULL_EMPLOYEE_NAME LIKE '%Yuriy%'
^ Problem is here ^and here ^and here
Add a space to the end of each of the strings.
The query is not correct-
While writing such queries inline, you must be sure that there should be proper spaces between the keywords-
You should debug the code and see what query variable returns
I think you should modify your query as follows (by giving spaces at the end of each line)
string query = "SELECT USER_ROLES.USER_ROLES_ID, EMPLOYEES.EMPLOYEE_ID, EMPLOYEES.EMPLOYEE_NAME, EMPLOYEES.SURNAME_1, EMPLOYEES.SURNAME_2, ROLES.NAME FROM USER_ROLES " +
"INNER JOIN EMPLOYEES ON USER_ROLES.USER_ID = EMPLOYEES.EMPLOYEE_ID " +
"INNER JOIN ROLES ON USER_ROLES.ROLE_ID = ROLES.ROLE_ID " +
"WHERE ROLES.IS_INTRANET_ONLY = 'N' AND EMPLOYEES.FULL_EMPLOYEE_NAME LIKE '%Yuriy%'";

convert sql query into lambda expression [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Here is my sql query i want to convert it into lambda expression
Select P.PlaceName As UnitNumber,
PB.PlaceName,
A.Locality,
A.SubLocality,
A.Sublocality_Level_1
from Listing L
inner join Place P ON L.PlaceId = P.Id
Inner Join Place PB ON PB.Id = P.ParentPlaceId
Inner Join [Address] A ON A.Id = PB.AddressId
Where L.Id =9
Thanks in advance .
Query expression is much simpler in this case. I don't recommend you to use lambdas with many joins
from l in db.Listing
join p in db.Place on l.PlaceId equals p.Id
join pb in db.Place on p.ParentPlaceId equals pb.Id
join a in db.Address on pb.AddressId equals a.Id
where l.Id == 9
select new {
UnitNumber = p.PlaceName,
pb.PlaceName,
a.Locality,
a.SubLocality,
a.Sublocality_Level_1
}
db is your context

Convert SQL to LINQ Query [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
Improve this question
I have the following SQL query and I need to have it in LINQ, I tried several things but I can not get it working.
Here is the SQL query
SELECT ST.Description, ST.STId, COUNT(SI.SIId) AS Expr1
FROM BP INNER JOIN
MbrBP ON BP.BPId = MbrBP.BPId INNER JOIN
SI ON BP.BPId = SI.BPId RIGHT OUTER JOIN
ST ON SI.STId = ST.STId
WHERE (BP.CountryId = 1) AND (BP.RegionId = 1) AND (MbrBP.MemberId = 1)
AND (SI.IsActive = 1)
GROUP BY ST.Description, ST.STId
UNION
SELECT ST.Description, ST.STId, COUNT(SI.STId) AS Expr1
FROM SI RIGHT OUTER JOIN
ST ON SI.STId = ST.STId
GROUP BY ST.Description, ST.STId

Categories