I'm using Entity Framework in order to connect to MySQL.
I have an incremental data which implements subversion technique. Each set of subversion record has the same LinkedId and separated by UpdatedTime.
My expectation is getting the latest version of each record from database. Thus, I write a linq statement like below:
public List<Entry> LoadFinalEntries(int rptId) {
return (from ent in ctx.Entries
where ent.ReportId == rptId
orderby ent.LinkedId, ent.UpdatedTime descending
group ent by ent.LinkedId into svnEnt
select svnEnt.FirstOrDefault()).ToList();
}
But at the run-time, it throws an EntityCommandCompilationException telling that "Specified method is not supported.". I know that method is FirstOrDefault, but cannot find anyway to fix it.
Please help me to find out another way to do.
What do you think using inner query?
return (from ent in ctx.Entries
where ent.ReportId == rptId &&
ent.UpdatedTime ==
(from inner in ctx.Entries
where inner.LinkedId == ent.LinkedId &&
inner.ReportId == rptId
select inner.UpdatedTime).Max()
select ent).ToList();
I've found a better solution after referring this article
public List<Entry> GetFinalEntries(int rptId) {
// Get the latest updated time for each linked id
var latestUpdatedTimes = from ent in ctx.Entries
where ent.ReportId == rptId
group ent by ent.LinkedId into svnEnt
select new { LinkedId = svnEnt.Key, UpdatedTime = svnEnt.Max(ent => ent.UpdatedTime) };
// Compare (by joining) to get full entries
var latestEntries = from ent in ctx.Entries
where ent.ReportId == rptId
join time in latestUpdatedTimes
on new { ent.LinkedId, ent.UpdatedTime } equals time
select ent;
return latestEntries.ToList();
}
Now it works fine.
Related
I have following table in my database and im accessing them through EF.
TestPack { id, name, type }
Sheets{ id, tp_id, date, rev }
Spool { id, sheet_id, date, rev, fabricated, installed }
which means a test pack has 1-M sheets and each sheet has 1-M spools. I want to get count of total Spools in the Test Pack, and count of spools that are fabricated, count of spools that are installed.
How do I get that through Linq query?
if I understand you right,you would like to have something like this
(from tp in ctx.TestPack
join st in ctx.Sheets on st.tp_id equals tp.id
join sl in ctx.Spool on sl.steet_id equals st.id
where tp.id == testPackId //you can change or delete this condition
select new {
Total = sl.Count() ,
FabricatedSpools = sl.Count(x=>x.fabricated == true),
InstalledSpools = sl.Count(x=>x.installed == true)
}).FisrtOrDefault();
Or maybe
(from tp in ctx.TestPack
join st in ctx.Sheets on st.tp_id equals tp.id
join sl in ctx.Spool on sl.steet_id equals st.id
where tp.id == testPackId //you can change or delete this condition
select new {
Total = sl.Count() ,
FabricatedSpools = (from s in sl
where s.fabricated == true
select s.Count()),
InstalledSpools = (from i in sl
where i.installed== true
select i.Count()),
}).FisrtOrDefault();
Not sure what you exact models are like but see below.
var testPackID = 2;//assuming
//assuming your DbContext is ctx;
var totalSpools = ctx.Spools.Count(x => x.Sheets.tp_id == testPackID);
var fabricatedSpools = ctx.Spools.Count(x => x.Sheets.tp_id == testPackID && x.fabricated);
var installedSpools = ctx.Spools.Count(x => x.Sheets.tp_id == testPackID && x.installed);
Sample data and query
I had generated the queries in SQL Server and hope you can do in LINQ. if you want specifically in LINQ let me know.
And can you please clarify whether you want result in 3 or all the 3 results in one.
Hope this helps.
Thank you.
There are three tables in the database that are relevant. Advocate, Vendor, and Advocate_Vendor.
The Advocate_Vendor table being the many to many link, has a vendorId and an advocateId.
My end goal is to get back a List<Advocate> object...a collection of advocates that belong to one Vendor. I wrote this:
var list = new List<Advocate>();
foreach (var vendorAdvocates in db.Advocate_Vendors)
{
if (vendorAdvocates.VendorId == vendorId)
{
list.Add(db.Advocates.SingleOrDefault(a => a.AdvocateId == vendorAdvocates.AdvocateId));
}
}
And then this:
var list = (from vendorAdvocates in db.Advocate_Vendors
where vendorAdvocates.VendorId == vendorId
select db.Advocates.SingleOrDefault(a =>
a.AdvocateId == vendorAdvocates.AdvocateId)).ToList();
Is this the best way? Seems wrong, like maybe there could be a more streamlined way to do this using a 'contains' keyword or something that looks a bit more readable...get all the vendor's advocates
thanks
Using a join between Advocate_Vendors and Advocates would be the right way of doing it.
var list = (from vendorAdvocates in db.Advocate_Vendors
join advocates in db.Advocates
on vendorAdvocates.AdvocateId equals advocates.AdvocateId
where vendorAdvocates.VendorId == vendorId
select advocates).ToList();
var list = (from vendorAdvocates in db.Advocate_Vendors
from advocate in db.Advocates
where vendorAdvocates.VendorId == vendorId &&
vendorAdvocates.AdvocateId = advocate.Id
select advocate)
.ToList();
If you set up your foreign keys and navigation properties properly, it should be possible to write this way, or something like it:
var list = (from vendorAdvocates in db.Advocate_Vendors
where vendorAdvocates.VendorId == vendorId
select db.Advocate).ToList();
I think I have got myself into a muddle with this one. My database looks like:
Locations [root]
Inspections [level1]
InspectionItems [level2]
InspectionReadings [level3]
Areas [level1]
Inspections [level2]
InspectionItems [level3]
InspectionReadings [level4]
each table is linked by Guid to the previous table and the Inspections table has nullable AreaID so that it can be a direct child of Locations.
What I need is
for each Location
{take all InspectionReadings entities
where location == location
sort date descending
return the top Entity details}
add the Location details and the InspectionReading details into a new table
return new table
the result should be a datagrid with a list of locations and their latest inspection reading date. each location should only appear once.
What I have managed is (this is in my DomainService.cs)
public IQueryable<LocationStatusList> GetLocationStatus()
{
var status = (from a in ObjectContext.Locations
from b in ObjectContext.InspectionReadings
orderby b.DateTaken descending, a.Name
select new LocationStatusList()
{
ID = b.ID,
LocationName = a.Name,
LastInspectionDate = b.DateTaken ?? DateTime.MinValue, // the data is nullable so it needs a value to return in that case
StatusNumber = b.Status ?? -1 // the data is nullable so it needs a value to return in that case
});
return status;
}
which returns the entire InspectionItems entities with their relevant location and although I've tried I can't find a way of doing what I need.
I would like to do all the code for this in the DomainService class but the only way I can think at the moment is to give the query each location name as a parameter from a viewmodel, return a single entity and create a new list and add each single entity.
Surely this can all be done in a LINQ query?
Final version, if I get it right:
var status = (from a in ObjectContext.Locations
select new {Location = a, LastReading = a.Inspections.SelectMany(i=>i.InspectionReadings).OrderBy(r=>r.PostDate).FirstOrDefault()};
Ok try this:
var result = from l in Locations
from i in l.Inspestions
from ii in i.InspestionItems
from ir in ii.InspectionReadings
group ir by l into g
select new {Location = g.Key, LastReading = g.OrderBy(r=>r.DateTaken).LastOrDefault() };
Last or default for the reason that default sort order is ascending and you need most recent(last).
Thank you #Val, the code I now have is :
public IQueryable<LocationStatusList> GetLocationStatus()
{
var status = (from a in ObjectContext.Locations
select new LocationStatusList
{
ID=a.ID,
Name=a.Name,
LastInspectionDate= (from b in ObjectContext.Inspections
from c in ObjectContext.InspectionReadings
where c.InspectionItemID==b.ID && b.LocationID==a.ID
orderby c.DateTaken descending
select c.DateTaken).FirstOrDefault()??DateTime.MinValue,
StatusNumber = (from b in ObjectContext.Inspections
from c in ObjectContext.InspectionReadings
where c.InspectionItemID==b.ID && b.LocationID==a.ID
orderby c.DateTaken descending
select c.Status).FirstOrDefault()??-1,
});
return status;
}
which gives precisely what I need. Thank you!
I have a sports database with a table groupmembers which have the fields ID, groupID and memberID. I get the memberID from a textbox called txtRemoveGroupMember and the groupID from a checkboxlist. Now I want to delete the rows which have both the groupID and the memberID.
I have tried this code:
foreach(ListItem listItem in cblRemoveMemberFromGroup.Items)
{
int memberid = Convert.ToInt32(txtRemoveGroupMember.Text);
int groupid = Convert.ToInt32(listItem.Value);
var removeFromGroup = from gm in dataContext.GroupMembers
where (gm.memberID == memberid) && (gm.groupID == groupid)
select gm;
dataContext.GroupMembers.DeleteOnSubmit(removeFromGroup);
dataContext.SubmitChanges();
}
But I get this error:
Error 7 Argument 1: cannot convert from 'System.Linq.IQueryable<GSI_side.GroupMember>' to 'GSI_side.GroupMember'
And this error:
Error 6 The best overloaded method match for 'System.Data.Linq.Table<GSI_side.GroupMember>.DeleteOnSubmit(GSI_side.GroupMember)' has some invalid arguments
Hope someone can help me figure this out!
You have to call .ToList()
var items = removeFromGroup.ToList();
foreach (var item in items)
dataContext.GroupMembers.DeleteOnSubmit(item);
For batch deletes I use this, because LINQ to SQL first loads the entire data which is going to be deleted, then it does the deletes each by each.
https://terryaney.wordpress.com/2008/04/14/batch-updates-and-deletes-with-linq-to-sql/
https://github.com/longday/LINQ-to-SQL-Batch-Updates-Deletes
removeFromGroup is still of type IQuerable.
You need to specify an actual GroupMember to delete.
You could use
GroupMember removeFromGroup = (from gm in dataContext.GroupMembers
where (gm.memberID == memberid) && (gm.groupID == groupid)
select gm).SingleOrDefault();
dataContext.GroupMembers.DeleteOnSubmit(removeFromGroup);
dataContext.SubmitChanges();
Alternatively, if your query returns a collection (from the looks of it, it won't since you are filtering by memberId) you could use
List<GroupMember> removeFromGroup = (from gm in dataContext.GroupMembers
where (gm.memberID == memberid) && (gm.groupID == groupid)
select gm).ToList();
dataContext.GroupMembers.DeleteAllOnSubmit(removeFromGroup);
dataContext.SubmitChanges();
var listToRemove=(from a in DB.Table
where a.Equals(id)
select a).ToList();
DB.Table.DeleteAllOnSubmit(listToRemove);
DB.SubmitChanges();
i'm very new to linq to sql and in need of a little assistance.
Basically i'm building a message board in C#. I have 3 database tables - basic info is as follows.
FORUMS
forumid
name
THREADS
threadid
forumid
title
userid
POSTS
postid
threadid
text
userid
date
Basically I want to bring back everything I need in one query. I want to list a page of THREADS (for a particular FORUM) and also display the number of POSTS in that THREAD row and when the last POST was for that THREAD.
At the moment i'm getting back all THREADS and then looping through each the result set and making calls to the POST table seperately for the POST count for a Thread and the Latest Post in that thread but obviously this will cause problems in terms of hitting the database as the Message Board gets bigger.
My Linq To SQL so far:
public IList<Thread> ListAll(int forumid)
{
var threads =
from t in db.Threads
where t.forumid == forumid
select t;
return threads.ToList();
}
basicaly i now need to get the number of POSTS in each thread and the date of the last post in each thread.
Any help would be most appreciated :)
EDIT
Hi guys. Thanks for tyour help so far. Basically i'm almost there. However, I left an important part out of my initial question in the fact that I need to retrieve the user name of the person making the last POST. Therefore I need to join p.userid with u.userid on the USERS table. So far I have the following but just need to amend this to join the POST table with the USER table:
public IList<ThreadWithPostInfo> ListAll(int forumid)
{
var threads = (from t in db.Threads
where t.forumid == forumid
join p in db.Posts on t.threadid equals p.threadid into j
select new ThreadWithPostInfo() { thread = t, noReplies = j.Count(), lastUpdate = j.Max(post => post.date) }).ToList();
return threads;
}
UPDATE:
public IList<ThreadWithPostInfo> ListAll(int forumid)
{
var threads = (from t in db.Threads
from u in db.Users
where t.forumid == forumid && t.hide == "No" && t.userid == u.userid
join p in db.Posts on t.threadid equals p.threadid into j
select new ThreadWithPostInfo() { thread = t, deactivated = u.deactivated, lastPostersName = j.OrderByDescending(post => post.date).FirstOrDefault().User.username, noReplies = j.Count(), lastUpdate = j.Max(post => post.date) }).ToList();
return threads;
}
I finally figured that part of it out with thanks to all of you guys :). My only problem now is the Search Results method. At the moment it is like this:
public IList<Thread> SearchThreads(string text, int forumid)
{
var searchResults = (from t in db.Threads
from p in db.Posts
where (t.title.Contains(text) || p.text.Contains(text)) && t.hide == "No"
&& p.threadid == t.threadid
&& t.forumid == forumid
select t).Distinct();
return searchResults.ToList();
}
Note that I need to get the where clause into the new linq code:
where (t.title.Contains(text) || p.text.Contains(text)) && t.hide == "No"
so incorporating this clause into the new linq method. Any help is gratefully received :)
SOLUTION:
I figured out a solution but I don't know if its the best one or most efficient. Maybe you guys can tell me because i'm still getting my head around linq. James I think your answer was closest and got me to near to where I wanted to be - thanks :)
public IList<ThreadWithPostInfo> SearchThreads(string text, int forumid)
{
var searchResults = (from t in db.Threads
from p in db.Posts
where (t.title.Contains(text) || p.text.Contains(text)) && t.hide == "No"
&& p.threadid == t.threadid
&& t.forumid == forumid
select t).Distinct();
//return searchResults.ToList();
var threads = (from t in searchResults
join p in db.Posts on t.threadid equals p.threadid into j
select new ThreadWithPostInfo() { thread = t, lastPostersName = j.OrderByDescending(post => post.date).FirstOrDefault().User.username, noReplies = j.Count(), lastUpdate = j.Max(post => post.date) }).ToList();
return threads;
}
May be Too many database calls per session ....
Calling the database,. whether to query or to write, is a remote call, and we want to reduce the number of remote calls as much as possible. This warning is raised when the profiler notices that a single session is making an excessive number of calls to the database. This is usually an indication of a potential optimization in the way the session is used.
There are several reasons why this can be:
A large number of queries as a result of a Select N + 1
Calling the database in a loop
Updating (or inserting / deleting) a large number of entities
A large number of (different) queries that we execute to perform our task
For the first reason, you can see the suggestions for Select N + 1. Select N + 1 is a data access anti-pattern where the database is accessed in a suboptimal way. Take a look at this code sample :
// SELECT * FROM Posts
var postsQuery = from post in blogDataContext.Posts
select post;
foreach (Post post in postsQuery)
{
//lazy loading of comments list causes:
// SELECT * FROM Comments where PostId = #p0
foreach (Comment comment in post.Comments)
{
//print comment...
}
}
In this example, we can see that we are loading a list of posts (the first select) and then traversing the object graph. However, we access the collection in a lazy fashion, causing Linq to Sql to go to the database and bring the results back one row at a time. This is incredibly inefficient, and the Linq to Sql Profiler will generate a warning whenever it encounters such a case.
The solution for this example is simple. Force an eager load of the collection using the DataLoadOptions class to specify what pieces of the object model we want to load upfront.
var loadOptions = new DataLoadOptions();
loadOptions.LoadWith<Post>(p => p.Comments);
blogDataContext.LoadOptions = loadOptions;
// SELECT * FROM Posts JOIN Comments ...
var postsQuery = (from post in blogDataContext.Posts
select post);
foreach (Post post in postsQuery)
{
// no lazy loading of comments list causes
foreach (Comment comment in post.Comments)
{
//print comment...
}
}
next is updating a large number of entities is discussed in Use Statement Batching, and can be achieved by using the PLinqO project, which is a set of extensions on top of Linq to Sql. How cool would it be to store items in cache as a group. Well, guess what! PLINQO is cool! When storing items in cache, just tell PLINQO the query result needs to belong to a group and specify the name. Invalidating cache is where the coolness of grouping really shows up. No coupling of cache and actions taken on that cache when they are in a group. Check out this example :
public ActionResult MyTasks(int userId)
{
// will be separate cache for each user id, group all with name MyTasks
var tasks = db.Task
.ByAssignedId(userId)
.ByStatus(Status.InProgress)
.FromCache(CacheManager.GetProfile().WithGroup("MyTasks"));
return View(tasks);
}
public ActionResult UpdateTask(Task task)
{
db.Task.Attach(task, true);
db.SubmitChanges();
// since we made an update to the tasks table, we expire the MyTasks cache
CacheManager.InvalidateGroup("MyTasks");
}
PLinqO supports the notion of query batching, using a feature called futures, which allow you to take several different queries and send them to the database in a single remote call. This can dramatically reduce the number of remote calls that you make and increase your application performance significantly.
cmiiw ^_^
public IList<Thread> ListAll(int forumid)
{
var threads =
from t in db.Threads
where t.forumid == forumid
select new
{
Thread = t,
Count = t.Post.Count,
Latest = t.Post.OrderByDescending(p=>p.Date).Select(p=>p.Date).FirstOrDefault()
}
}
Should be something like that
I think what you're really looking for is this:
var threadsWithPostStats = from t in db.Threads
where t.forumid == forumid
join p in db.Posts on t.threadid equals p.threadid into j
select new { Thread = t, PostCount = j.Count(), LatestPost = j.Max(post => post.date) };
Per your comment and updated question, I'm adding this restatement:
var threadsWithPostsUsers = from t in db.Threads
where t.forumid == forumid
join p in db.Posts on t.threadid equals p.threadid into threadPosts
let latestPostDate = threadPosts.Max(post => post.date)
join post in db.Posts on new { ThreadID = t.threadid, PostDate = latestPostDate } equals new { ThreadID = post.threadid, PostDate = post.date} into latestThreadPosts
let latestThreadPost = latestThreadPosts.First()
join u in db.Users on latestThreadPost.userid equals u.userid
select new { Thread = t, LatestPost = latestThreadPost, User = u };
Wouldn't hurt to get familiar with group by in LINQ and aggregates (Max, Min, Count).
Something like this:
var forums = (from t in db.Threads
group t by t.forumid into g
select new { forumid = g.Key, MaxDate = g.Max(d => d.ForumCreateDate) }).ToList();
Also check out this article for how to count items in a LINQ query with group by:
LINQ to SQL using GROUP BY and COUNT(DISTINCT)
LINQ aggregates:
LINQ Aggregate with Sub-Aggregates