This question already has answers here:
How to do joins in LINQ on multiple fields in single join
(13 answers)
Closed 2 years ago.
This is My Query in Linq
VAR x = FROM n IN _db.tbl_maint_activity_material_details
JOIN availablity IN _db.tbl_maint_item_availability ON n.mamd_lotno equals availablity.mia_lot_no && n. ////
Here im not able to countine my Code Please Help me
This is My SQL query
SELECT *
FROM tbl_maint_activity_material_details AS _material
JOIN tbl_maint_item_availability AS availablity
ON _material.mamd_maint_item_id = availablity.mia_maint_item_id
AND _material.mamd_lotno = availablity.mia_lot_no
WHERE _material.mamd_status = 24
To accomplish that, you have to build an object and use equals.
Something like:
on new { mia_lot_no = n.mamd_lotno, mamd_lotno = n.mamd_lotno } equals new { mia_lot_no = availablity.mia_lot_no, mamd_lotno = ... }
So that the equals will result true if the whole object is equal.
Related
This question already has answers here:
An anonymous type cannot have multiple properties with the same name
(2 answers)
Closed 6 years ago.
I have a method with which is the following:
using (ubmmsEntities db = new ubmmsEntities())
{
var result = (from l in db.log_documents
join t in db.teams on l.op_user_team equals t.id
where l.tracking_id == trackingID
select new { l.op_code, l.op_date, l.op_description, l.op_refer_code, l.op_refer_comments, t.team_name, l.id });
return result.
Now I have to add another column it, and in simple SQL all I would need is to add two more inner joins, but I am quite lost how to do it in EF a the below results a error under t2.team_name "An anonymous type cannot have multiple properties with the same name".
using (ubmmsEntities db = new ubmmsEntities())
{
var result = (from l in db.log_documents
join t in db.teams on l.op_user_team equals t.id
join r in db.refers_codes on l.op_refer_code equals r.code
join t2 in db.teams on r.id equals t2.id
where l.tracking_id == trackingID
select new { l.op_code, l.op_date, l.op_description, l.op_refer_code, l.op_refer_comments, t.team_name, l.id, t2.team_name });
return result.ToList();
}
So, I looked over here and there, and found this thread, but I am unable to figure out how to apply the solution to my method. I googled for "EF naming types" and similar to try to understand what is being asked from me, but honestly I was unable to figure out by myself.
So, I believe the problem is because EF wants me to set a unique name to my db.team... which I thought I'd given it by calling it t2. This approach does work on SQL, but I do not understand how to apply the same to my method. I did find some clues such as new{t2=t.id} but different errors starting to pop-up in different places.
Help/Directions please?
You can give names to properties in your projectction ie
.Select(x => new { firstItem = x.y, secondItem = x.x });
You can use this to get around your problem.
Or, as you are doing it:
select new { firstItem = x.y, secondItem = x.x } .....
This question already has answers here:
Where IN clause in LINQ [duplicate]
(8 answers)
Linq to Entities - SQL "IN" clause
(9 answers)
Closed 8 years ago.
I am trying to mimic an IN() clause commonly used in SQL and use it in my LINQ statement.
I see that there is an overload for Contains() that takes an IEnumerable collection. I have tried passing in ILIST and Dictionary but neither is correct.
How do I accomplish this?
Thanks, much appreciated.
SQL
select
oec.OnlineEducationCourseId,
oec.CourseTitle,COUNT(oec.CourseTitle) as CourseCount
from OnlineEducationRegistration as oer
join OnlineEducationCourse oec on oec.OnlineEducationCourseId = oer.OnlineEducationCourseId
where oer.ClubId IN('K20','B67','P89')
and DateCompleted between '2013-01-01' and '2014-01-01'
group by oec.CourseTitle,oec.OnlineEducationCourseId
Same SQL query in LINQ
var r = (from oer in db.OnlineEducationRegistrations
join oec in db.OnlineEducationCourses on oer.OnlineEducationCourseId equals
oec.OnlineEducationCourseId
where (oer.ClubId.Contains(some IEnumerable collection here) && oer.DateCompleted >= start.Date && oer.DateCompleted <= end.Date)
group new { oec, oer } by new { oec.CourseTitle, oec.OnlineEducationCourseId }).ToList();
This question already has answers here:
How to do joins in LINQ on multiple fields in single join
(13 answers)
Closed 8 years ago.
I'm trying to create a linq join statement which joins an object from a table based on two conditions
MESSAGES
======
ID (int)
UserID (Guid)
MESSAGEPART
======
MessageID (int)
IsPlaintext (bool)
MessageContent (nvarchar(max))
Here's the query I want to write, essentially:
var messages = from m in db.Message
join p in db.MessagePart on m.ID equals p.MessageID
and p.IsPlaintext equals false
Unfortunately, this doesn't work. This is the best I can do.
var messages = from m in db.Message
join p in
(from x in db.MessagePart where x.IsPlaintext == false select x)
on m.ID equals p.MessageID
This seems a bit longwinded. Is there a more elegant way of achieving it?
An elegant solution.
var messages = from m in db.Message
join p in db.MessagePart on m.ID equals p.MessageID
where p.IsPlaintext == false
You could try this one:
var messages = from m in db.Message
join p in db.MessagePart
on new { m.ID, false } equals { p.MessageID, p.IsPlaintext }
or you could try this one:
var messages = db.Message.Join(db.MessagePart.Where(x=>x.IsPlainText==false),
x=>x.ID,
y=>y.Id,
(x,y)=>new {});
Inside the new { } you will declare the properties of the anonymous type you select.
ps. If you update your post and show there which fields you want to select, I could update also mine.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Multiple “order by” in LINQ
I want to order some dataset by some field AND THEN some other field.
Using lambda expressions this would be easy (OrderBy.ThenBy), but how to do it when I have a syntax like this:
int maxQueries = int.MaxValue;
// finds the most search for queries
var ordered = from p in searchLogs
where !string.IsNullOrEmpty(p.SearchQuery)
group p by new
{
SearchQuery = p.SearchQuery
}
into pgroup
let count = pgroup.Count()
orderby count descending
select new
{
Count = count,
SearchQuery = pgroup.Key.SearchQuery
};
I can't seem to find a way which works after the decending keyword (like orderby count descending then searchquery)..
Any ideas?
Put a comma after descending and specify the next thing to sort by (optionally adding descending) and so on
Just to add some code to mlorbetske answer, if you have Customer class like this:
public class Customer
{
public string Name;
public DateTime MemberSince;
}
You could then perform an ordering like this:
var q = from c in Customers
orderby c.MemberSince.Date descending, c.Name
select c;
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Linq to SQL “not like” operator
How can I write a dynamic linq query with using not contains?
I use .Contains() instead of like. But what should I use instead of not like?
just use ! before the contains condition. Like
var myProducts = from p in products
where !productList.Contains(p.ID)
select p;
Some thing like this should help...
YourDataContext dc = new YourDataContext();
var query =
from c in dc.Customers
where !(from o in dc.Orders
select o.CustomerID)
.Contains(c.CustomerID)
select c;
Use ! operator. Like this:
private List<int> iList = new List<int>
{
1,2,3,4,5,6,7,8,9
};
if (!iList.Contains(888))
{
}