Problem with Linq query .net MVC - sql

public ActionResult Performances(string id)
{
var query =
from f in _db.Production
join g in _db.Run on f.show equals g.Production.show
join l in _db.Performance on g.startDate equals l.runStartDate
where f.show == id
select new ShowPerformance
{
Venuename = g.venue,
Showname = f.show,
RunStart = g.startDate,
RunEnd = g.endDate,
PerformanceDate = l.performanceDate,
PerformanceTime = l.performanceTime
};
return View(query.ToList());
}
The query can not distuingish between a performance in ShowA run1 and Show A run2 it just duplicates all performances ShowA run1 and Show A run2

I think the problem might be how you join Performance to Run/Production
var query =
from f in _db.Production
join g in _db.Run on new {f.show, f.year} equals new {g.show, g.year}
join l in _db.Performance on new {g.venue, g.startDate} equals new {l.venue, l.runStartDate}
where f.show == id
select new ShowPerformance
{
Venuename = g.venue,
Showname = f.show,
RunStart = g.startDate,
RunEnd = g.endDate,
PerformanceDate = l.performanceDate,
PerformanceTime = l.performanceTime
};
without something like the on g.runId equals l.runId then you will get all the performances for all the productions/runs.

Related

Time Out when Executing a SQL Query. Any Solution?

We use a google script to get data from a SQL database into a spreadsheet. There is a time-driven trigger for the function.
Every time I run the function, there is a time out.
Exception: The query has timed out
When I run the function a second time, it usually works fine. Does anybody has a solution for that?
The address and password of the Database is not shown here.
function onOpen() {
var spreadsheet = SpreadsheetApp.getActive();
var menuItems = [
{name: 'JTL Import', functionName: 'readData'}
];
spreadsheet.addMenu('JTL-Import', menuItems);
}
// Replace the variables in this block with real values.
var address = 'XXXX'; //ex. '10.1.1.1:1433'
var user = 'TAXDOO';
var userPwd = 'XXX';
var db = 'eazybusiness';
var dbUrl = 'jdbc:sqlserver://' + address + ';databaseName=' + db;
function StockUnitsSold30days() {
var conn = Jdbc.getConnection(dbUrl, user, userPwd);
var stmt = conn.createStatement();
var results = stmt.executeQuery("SELECT a.cArtNr SKU, aaI18n.cwertvarchar [Cut_ID], CONVERT(FLOAT, ISNULL(wmsLager.fbestand, 0)) [Stock_WMS], CONVERT(FLOAT, ISNULL(StreckenLager.fbestand, 0)) [Stock_Streckenlager], CONVERT(FLOAT, ISNULL(fba.fanzahlfba, 0)) [Stock_FBA], CONVERT(FLOAT, ISNULL(PendingLager.fbestand, 0)) [Stock_Pending], CONVERT(FLOAT, ISNULL(WmsAusgang.Anzahl, 0)) [Units_WMS_Outgoing], CONVERT(FLOAT, ISNULL(fbaAusgang.SumAnzahl, 0)) [Units_FBA_Outgoing],CONVERT(FLOAT, ISNULL(MengeAufWarenEingang.fAnzahlAktuell,0)) [Stock_Wareneingangsplatz] FROM dbo.tartikel a INNER JOIN dbo.tartikelattribut aa ON aa.kartikel = a.kartikel INNER JOIN dbo.tartikelattributsprache aaI18n ON aaI18n.kartikelattribut = aa.kartikelattribut AND aaI18n.ksprache = 0 INNER JOIN dbo.tattributsprache attributI18n ON attributI18n.kattribut = aa.kattribut AND attributI18n.ksprache = 0 AND attributI18n.cname = 'CutDesign_ID' LEFT OUTER JOIN dbo.vlagerbestandfba fba ON a.kArtikel = fba.kartikel LEFT OUTER JOIN dbo.vlagerbestandprolageralle wmsLager ON wmsLAger.kartikel = a.kArtikel AND wmsLager.kwarenlager = 4 LEFT OUTER JOIN dbo.vlagerbestandprolageralle StreckenLager ON StreckenLager.kartikel = a.kArtikel AND Streckenlager.kwarenlager = 5 LEFT OUTER JOIN dbo.vlagerbestandprolageralle PendingLager ON PendingLager.kartikel = a.kArtikel AND PendingLager.kwarenlager = 7 LEFT OUTER JOIN (SELECT a.kArtikel, SUM(wla.fAnzahl) Anzahl FROM dbo.tBestellung b JOIN dbo.tbestellpos bp ON bp.tBestellung_kBestellung = b.kBestellung JOIN dbo.trechnung r ON r.tBestellung_kBestellung = b.kBestellung JOIN dbo.tLieferschein l ON l.kBestellung = b.kBestellung JOIN dbo.tLieferscheinPos lp ON lp.kBestellPos = bp.kBestellPos JOIN dbo.tWarenLagerAusgang wla ON wla.kLieferscheinPos = lp.kLieferscheinPos JOIN dbo.tArtikel a ON bp.tArtikel_kArtikel = a.kArtikel WHERE DATEDIFF(dd, b.derstellt, GETDATE()) <= 30 GROUP BY a.kArtikel ) AS WmsAusgang ON WmsAusgang.kArtikel = a.kArtikel LEFT OUTER JOIN (SELECT map.kArtikel, SUM(abp.nQuantityPurchased) SumAnzahl FROM dbo.pf_amazon_bestellung ab JOIN dbo.pf_amazon_bestellungpos abp ON abp.kAmazonBestellung = ab.kAmazonBestellung JOIN dbo.tBestellung b ON b.cInetBestellNr = ab.cOrderId JOIN dbo.trechnung r ON r.tBestellung_kBestellung = b.kBestellung JOIN dbo.pf_amazon_angebot_mapping map ON map.cSellerSKU = abp.cArtNr WHERE nFBA = 1 AND cOrderStatus = 'Shipped' AND DATEDIFF(dd, b.derstellt, GETDATE()) <= 30 GROUP BY map.kArtikel) FbaAusgang ON FbaAusgang.kArtikel = a.kArtikel LEFT OUTER JOIN (SELECT DISTINCT ta.kArtikel, twle.fAnzahlAktuell FROM dbo.tArtikel ta JOIN dbo.tWarenLagerEingang twle ON twle.kArtikel = ta.kArtikel JOIN dbo.tWarenlagerPlatz twlp ON twle.kWarenLagerPlatz = twlp.kWarenLagerPlatz WHERE twlp.cName = 'Wareneingangsplatz') MengeAufWarenEingang ON MengeAufWarenEingang.kArtikel = a.kArtikel");
var metaData=results.getMetaData();
var numCols = metaData.getColumnCount();
var spreadsheet = SpreadsheetApp.getActive();
var sheet = spreadsheet.getSheetByName("Test");
//you can use the following line to get the active sheet
//var sheet = SpreadsheetApp.getActiveSheet();
sheet.clearContents();
var arr=[];
for (var col = 0; col < numCols; col++) {
arr.push(metaData.getColumnName(col + 1));
}
sheet.appendRow(arr);
while (results.next()) {
arr=[];
for (var col = 0; col < numCols; col++) {
arr.push(results.getString(col + 1));
}
sheet.appendRow(arr);
}
results.close();
stmt.close();
sheet.autoResizeColumns(1, numCols+1);
}```
Runs slow first time and then fast right after can be a sign of a poorly optimized query.
If there' a table scan, the data might be loaded into cache the first time. It's in cache the second time.
That's a lot of JOINS and a big query. I would recommend a stored procedure if it's an option. The query plan might indicate missing indexes. It should also show the most expensive operations.
If 30 days is is a small fraction of the data and derstellt has an index, then compare the column directly to a date. Wrapping a DATEDIFF around the column prevents effective use of the index on that column.
DECLARE #TodayMinus29DaysMidnight datetime = CAST(GETDATE() - 29 as date)
SELECT...
WHERE b.derstellt < #TodayMinus29DaysMidnight
I would also verify the derived table with the DISINCT. Would a GROUP BY be better?

Powershell Single row datatable return to fill combobox

I'm in Powershell v4.0. I have a dropdown on my UI that I am filling with data from my database. I call a function that grabs the data like so:
$ConfigDynamicContractID.ItemsSource = (GetDynamicContracts).DefaultView
On the backend it looks like this:
function GetDynamicContracts{
$ContractQuery = "select con.ContractNumber as Name, c.ContractID from foo..campaign c inner join foo..campaigntemplate ct on c.campaignid = ct.campaignid
inner join foo..template t on ct.templateid = t.templateid inner join bar..contracts con on con.ContractID = c.ContractID
where c.ProductType = 'INTRO' and t.FileName = 'Intro Dynamic Template' and c.DeleteFlag = 0"
$ContractResult = ExecuteSelect "Bar" $ContractQuery
return , $ContractResult
}
This code works fine. I was then told that the tables referenced above can't talk to each other so I have to do individual calls to each and filter in powershell. So here is my code for that:
function GetDynamicContracts{
$CampaignQuery = "Select c.* From foo..Campaign c join foo..CampaignTemplate ct on c.CampaignID = ct.CampaignID
join foo..Template t on ct.TemplateID = t.TemplateID
Where c.ProductType = 'INTRO' and t.FileName = 'Intro Dynamic Template' and c.DeleteFlag = 0"
$CampaignResults = ExecuteSelect "foo" $CampaignQuery
$ContractIDs = New-Object System.Collections.ArrayList
foreach($row in $CampaignResults.Rows){
$ContractIDs.Add($row.ContractID)
}
$ContractIDs = $ContractIDs -join ","
$PHQuery = "Select ContractID, ContractNumber as Name From Contracts Where ContractID in ($ContractIDs)"
$PHResults = ExecuteSelect "Bar" $PHQuery
#Log $MyInvocation.MyCommand $PSBoundParameters
return ,$PHResults
}
This code for some reason does not work when returning a single row. But if I break this function call into two separate, then it works. See below:
function GetDynamicContracts{
$ContractIDs = GetDynamicCampaign
$ContractIDs = $ContractIDs[1]
$PHQuery = "Select ContractID, ContractNumber as Name From Contracts Where ContractID in ($ContractIDs)"
$PHResults = ExecuteSelect "PhytelMaster" $PHQuery
Log $MyInvocation.MyCommand $PSBoundParameters
return ,$PHResults
}
function GetDynamicCampaign{
$CampaignQuery = "Select c.* From Campaign..Campaign c join Campaign..CampaignTemplate ct on c.CampaignID = ct.CampaignID
join Campaign..Template t on ct.TemplateID = t.TemplateID
Where c.ProductType = 'INTRO' and t.FileName = 'Intro Dynamic Template' and c.DeleteFlag = 0"
$CampaignResults = ExecuteSelect "Campaign" $CampaignQuery
$ContractIDs = New-Object System.Collections.ArrayList
foreach($row in $CampaignResults.Rows){
$ContractIDs.Add($row.ContractID)
}
$ContractIDs = $ContractIDs -join ","
return $ContractIDs
}
It seems that making two execute selects in a single function causes the ',' to fail to do what it does when returning the data. Can anyone explain why?? Thanks ahead of time and since I have a workaround there is no rush.
As #TheMadTechnician and #wOxxOm point out ArrayList spams the indexes into the return and return, returns everything that gets spammed in the function. So when I expected $PHResults I also got the ArrayList spam. I could use something other than the ArrayList or I also found that if I wrap my function code in $null = #{ 'Code' } and then return the variable I want, all I get is that variable. It's ugly but it works.

How to Join two tables showing all records Where Table A is Not In Table B

I have a email marketing web application. I want to show which email contacts in (Table B) are not showing up in EmailContacts_Campaign (Table A). In addition, I want to filter table A by the CampaignId field. When I run the below code I get 0 records, yet I know there are a couple of thousand records there. Can anyone tell me where I am messing up?
SELECT * FROM TableA
LEFT JOIN TableB
ON TableA.EmailContactId = TableB.EmailContactId
WHERE TableA.CampaignId = 1
AND TableB.EmailContactId IS NULL
ORDER BY TableB.EmailContactId DESC
I want to show all email contacts in the EmailContact Table that are not showing up in the EmailContactCampaign table. Here is the actual code:
public List<EmailContact> GetNotAssignedContactsForCampaign(int campaignId)
{
string sqlCommand = "SELECT * FROM EmailContactCampaign LEFT JOIN EmailContact";
sqlCommand += " ON EmailContactCampaign.EmailContact_EmailContactId = EmailContact.EmailContactId";
sqlCommand += " WHERE EmailContactCampaign.EmailContact_EmailContactId = " + campaignId.ToString() AND EmailContact.EmailContactId IS NULL ;
sqlCommand += " ORDER BY EmailContact.EmailContactId DESC";
var emailContacts = new List<EmailContact>();
string CS = db.Database.Connection.ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
con.Open();
SqlCommand cmd = new SqlCommand(sqlCommand, con);
//Create sql datareader
using (SqlDataReader sqlDataReader = cmd.ExecuteReader())
{
while (sqlDataReader.Read())
{
var emailContact = new EmailContact();
emailContact.Assigned = ((bool)sqlDataReader["Assigned"]);
emailContact.Cell1 = _crypto.DecryptAndSanitize(sqlDataReader["Cell1"] as string);
emailContact.Cell2 = _crypto.DecryptAndSanitize(sqlDataReader["Cell2"] as string);
emailContact.City = _crypto.DecryptAndSanitize(sqlDataReader["City"] as string);
emailContact.Company = _crypto.DecryptAndSanitize(sqlDataReader["Company"] as string);
emailContact.EmailAddress = _crypto.DecryptAndSanitize(sqlDataReader["EmailAddress"] as string);
emailContact.EmailContactId = (int)sqlDataReader["EmailContactId"];
emailContact.FullName = _crypto.DecryptAndSanitize(sqlDataReader["FullName"] as string);
emailContact.Hold = (bool)sqlDataReader["Hold"];
emailContact.Phone1 = _crypto.DecryptAndSanitize(sqlDataReader["Phone1"] as string);
emailContact.Phone2 = _crypto.DecryptAndSanitize(sqlDataReader["Phone2"] as string);
emailContact.State = _crypto.DecryptAndSanitize(sqlDataReader["State"] as string);
emailContact.Status = (Status)sqlDataReader["Status"];
emailContact.Zip = _crypto.DecryptAndSanitize(sqlDataReader["Zip"] as string);
emailContacts.Add(emailContact);
}
}
return (emailContacts);
}
}
Have you tried this?
SELECT * FROM tableB WHERE EmailContactId NOT IN (SELECT EmailContactId FROM tableA)
i think you got 0 probably because of this AND TableB.EmailContactId IS NULL
Please try this one
SELECT * FROM TableA
LEFT JOIN TableB
ON TableA.EmailContactId = TableB.EmailContactId
WHERE TableA.CampaignId = 1
ORDER BY TableB.EmailContactId DESC
I'm sorry my question was not clear enough. Did some digging and found the answer on another post. Sorry but I accidentally closed it and can't find it again. Anyway, here is my implementation of it.
SELECT * FROM EmailContact
WHERE NOT EXISTS
(SELECT * FROM EmailContactCampaign WHERE EmailContactCampaign.EmailContact_EmailContactId = EmailContact.EmailContactId AND EmailContactCampaign.Campaign_CampaignId = 1)
If i understood your question correctly, you are looking for B's that are not in A. But your query will return A's that are not in B. Turn it aroung (tableB left join tableA where a... is NULL)
Your problem was that you had it the wrong way around: your query would return all contacts from EmailContactCampaign that were not in EmailContact.
The correct solution for your problem would look like this:
SELECT * FROM EmailContact
WHERE EmailContactId NOT IN (
SELECT EmailContact_EmailContactId FROM EmailContactCampaign
WHERE Campaign_CampaignId = ?
)
ORDER BY EmailContact.EmailContactId DESC

How can I select data with linq to sql from another query?

How can I select data with such linq to sql code, it's something wrong, i must compare WHERE, from st1 and st2, but something wrong. Also spaller didn't light this variables in where
var st1 = (from a in db.RouteDetail
where a.Station == "Гродно"
select new
{
a.Route,
});
var st2 = (from c in db.RouteDetail
where c.Station == "Лида"
select new
{
c.Route,
});
var res = (from d in db.RouteDetail
where st1.Route == st2.Route
select d);
Help me to do this, please.
I think what you mean to do is something like this :
var st1 = (from a in db.RouteDetail
where a.Station == "Гродно"
select new
{
a.Route,
});
var st2 = (from c in db.RouteDetail
where c.Station == "Лида"
select new
{
c.Route,
});
Both st1 and st2 now contain IQueryable types which allow you to enumerate over them. I'm assuming that your two queries only return one result in which case:
var station1 = st1.FirstorDefault();
var station2 = st2.FirstorDefault();
var res = (from d in db.RouteDetail
where station1.Route == station2.Route
select d);
Will do what you need it to do (provided they return a result theres no safety here for null exceptions). If however there is more than one result, it will only return the first match. In which case you will need to work out all the possible compositions from the values in st1 and st2

How do I map lists of nested objects with Dapper

I'm currently using Entity Framework for my db access but want to have a look at Dapper. I have classes like this:
public class Course{
public string Title{get;set;}
public IList<Location> Locations {get;set;}
...
}
public class Location{
public string Name {get;set;}
...
}
So one course can be taught at several locations. Entity Framework does the mapping for me so my Course object is populated with a list of locations. How would I go about this with Dapper, is it even possible or do I have to do it in several query steps?
Alternatively, you can use one query with a lookup:
var lookup = new Dictionary<int, Course>();
conn.Query<Course, Location, Course>(#"
SELECT c.*, l.*
FROM Course c
INNER JOIN Location l ON c.LocationId = l.Id
", (c, l) => {
Course course;
if (!lookup.TryGetValue(c.Id, out course))
lookup.Add(c.Id, course = c);
if (course.Locations == null)
course.Locations = new List<Location>();
course.Locations.Add(l); /* Add locations to course */
return course;
}).AsQueryable();
var resultList = lookup.Values;
See here https://www.tritac.com/blog/dappernet-by-example/
Dapper is not a full blown ORM it does not handle magic generation of queries and such.
For your particular example the following would probably work:
Grab the courses:
var courses = cnn.Query<Course>("select * from Courses where Category = 1 Order by CreationDate");
Grab the relevant mapping:
var mappings = cnn.Query<CourseLocation>(
"select * from CourseLocations where CourseId in #Ids",
new {Ids = courses.Select(c => c.Id).Distinct()});
Grab the relevant locations
var locations = cnn.Query<Location>(
"select * from Locations where Id in #Ids",
new {Ids = mappings.Select(m => m.LocationId).Distinct()}
);
Map it all up
Leaving this to the reader, you create a few maps and iterate through your courses populating with the locations.
Caveat the in trick will work if you have less than 2100 lookups (Sql Server), if you have more you probably want to amend the query to select * from CourseLocations where CourseId in (select Id from Courses ... ) if that is the case you may as well yank all the results in one go using QueryMultiple
No need for lookup Dictionary
var coursesWithLocations =
conn.Query<Course, Location, Course>(#"
SELECT c.*, l.*
FROM Course c
INNER JOIN Location l ON c.LocationId = l.Id
", (course, location) => {
course.Locations = course.Locations ?? new List<Location>();
course.Locations.Add(location);
return course;
}).AsQueryable();
I know I'm really late to this, but there is another option. You can use QueryMultiple here. Something like this:
var results = cnn.QueryMultiple(#"
SELECT *
FROM Courses
WHERE Category = 1
ORDER BY CreationDate
;
SELECT A.*
,B.CourseId
FROM Locations A
INNER JOIN CourseLocations B
ON A.LocationId = B.LocationId
INNER JOIN Course C
ON B.CourseId = B.CourseId
AND C.Category = 1
");
var courses = results.Read<Course>();
var locations = results.Read<Location>(); //(Location will have that extra CourseId on it for the next part)
foreach (var course in courses) {
course.Locations = locations.Where(a => a.CourseId == course.CourseId).ToList();
}
Sorry to be late to the party (like always). For me, it's easier to use a Dictionary, like Jeroen K did, in terms of performance and readability. Also, to avoid header multiplication across locations, I use Distinct() to remove potential dups:
string query = #"SELECT c.*, l.*
FROM Course c
INNER JOIN Location l ON c.LocationId = l.Id";
using (SqlConnection conn = DB.getConnection())
{
conn.Open();
var courseDictionary = new Dictionary<Guid, Course>();
var list = conn.Query<Course, Location, Course>(
query,
(course, location) =>
{
if (!courseDictionary.TryGetValue(course.Id, out Course courseEntry))
{
courseEntry = course;
courseEntry.Locations = courseEntry.Locations ?? new List<Location>();
courseDictionary.Add(courseEntry.Id, courseEntry);
}
courseEntry.Locations.Add(location);
return courseEntry;
},
splitOn: "Id")
.Distinct()
.ToList();
return list;
}
Something is missing. If you do not specify each field from Locations in the SQL query, the object Location cannot be filled. Take a look:
var lookup = new Dictionary<int, Course>()
conn.Query<Course, Location, Course>(#"
SELECT c.*, l.Name, l.otherField, l.secondField
FROM Course c
INNER JOIN Location l ON c.LocationId = l.Id
", (c, l) => {
Course course;
if (!lookup.TryGetValue(c.Id, out course)) {
lookup.Add(c.Id, course = c);
}
if (course.Locations == null)
course.Locations = new List<Location>();
course.Locations.Add(a);
return course;
},
).AsQueryable();
var resultList = lookup.Values;
Using l.* in the query, I had the list of locations but without data.
Not sure if anybody needs it, but I have dynamic version of it without Model for quick & flexible coding.
var lookup = new Dictionary<int, dynamic>();
conn.Query<dynamic, dynamic, dynamic>(#"
SELECT A.*, B.*
FROM Client A
INNER JOIN Instance B ON A.ClientID = B.ClientID
", (A, B) => {
// If dict has no key, allocate new obj
// with another level of array
if (!lookup.ContainsKey(A.ClientID)) {
lookup[A.ClientID] = new {
ClientID = A.ClientID,
ClientName = A.Name,
Instances = new List<dynamic>()
};
}
// Add each instance
lookup[A.ClientID].Instances.Add(new {
InstanceName = B.Name,
BaseURL = B.BaseURL,
WebAppPath = B.WebAppPath
});
return lookup[A.ClientID];
}, splitOn: "ClientID,InstanceID").AsQueryable();
var resultList = lookup.Values;
return resultList;
There is another approach using the JSON result. Even though the accepted answer and others are well explained, I just thought about an another approach to get the result.
Create a stored procedure or a select qry to return the result in json format. then Deserialize the the result object to required class format. please go through the sample code.
using (var db = connection.OpenConnection())
{
var results = await db.QueryAsync("your_sp_name",..);
var result = results.FirstOrDefault();
string Json = result?.your_result_json_row;
if (!string.IsNullOrEmpty(Json))
{
List<Course> Courses= JsonConvert.DeserializeObject<List<Course>>(Json);
}
//map to your custom class and dto then return the result
}
This is an another thought process. Please review the same.