SQLite id field - autofill - ios

I've added a new table to my DB:
CREATE TABLE myTable (
myId ID NOT NULL,
aNumber INTEGER NOT NULL,
anUniqueTextId TEXT NOT NULL,
aText TEXT,
PRIMARY KEY(myId, anUniqueTextId) ON CONFLICT IGNORE,
FOREIGN KEY(anUniqueTextId, aNumber) REFERENCES otherTable(otherTableId, otherTableValue2) ON DELETE CASCADE
);
Now I want to insert values without manually finding out which index should I put in myId field:
[myDatabase inTransaction:^(FMDatabase *db, BOOL *rollback) {
[db executeUpdate:#"INSERT INTO myTable VALUES(?,?,?)",
// #(myData.myId), //I don't want to insert it manually
#(aNumber),
myData.anUniqueTextId,
myData.aText];
if (db.lastErrorCode) {
NSLog(#"Huston, we've got a problem: %#", db.lastError.localizedDescription);
}
}];
Ofc, I get here an error telling:
table myTable has 4 columns but 3 values were supplied
Now the question is how to insert the data to make this field autoinsert myId? I'm not sure if my insert code is invalid or CREATE TABLE statement.
-- UPDATE --
I've updated create statement to correct one:
CREATE TABLE myTable (
myId INTEGER PRIMARY KEY DEFAULT 0 NOT NULL,
aNumber INTEGER NOT NULL,
anUniqueTextId TEXT NOT NULL,
aText TEXT,
FOREIGN KEY(anUniqueTextId, aNumber) REFERENCES otherTable(otherTableId, otherTableValue2) ON DELETE CASCADE
);

With respect to your comment you need to specify the PRIMARY KEY with a NULLvalue in the QUERY:
[db executeUpdate:#"INSERT INTO myTable VALUES(?,?,?,?)",
NULL,
#(aNumber),
myData.anUniqueTextId,
myData.aText];
That way the database will fill in the primary key and autoincrement it (SQL Lite specs for PRIMARY KEY).

Here you need to change the schema of table.
To auto insert into myId field, you must have to add constraint AUTOINCREMENT when you create table. Also this field must be use as primary key. So now the schema for table is like:
CREATE TABLE myTable (
myId INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL
aNumber INTEGER NOT NULL,
anUniqueTextId TEXT NOT NULL,
aText TEXT,
FOREIGN KEY(anUniqueTextId, aNumber) REFERENCES otherTable(otherTableId, otherTableValue2) ON DELETE CASCADE
);

Related

How to join these Tables with

I want to join table Saleperson with Table Salevolume. The Logic of the join is:
Table Saleperson has key: itemfrom and itemto, Table Salevolume has Key: Item. When the key "Item" from Salevolume is between the key "itemfrom" and "itemto" of saleperson, then i will make sume of salevolume, group by Saleperson
The Key "accountfrom" and "accountto" and "item" have sometime character at the end
Can you please help me ?
Thanks
CREATE TABLE [dbo].[Saleperson](
[Saleperson] [nvarchar](3) NULL,
[itemfrom] [nvarchar](4) NULL,
[itemto] [nvarchar](4) NULL
)
insert into [dbo].[Saleperson]
values
('A','111H','112H'),
('B','122G','125G'),
('C','134F','137F'),
('D','117','119'),
CREATE TABLE [dbo].[Salevolume](
[Item] [nvarchar](6) NULL,
[Salevolume] [int] NULL
)
insert into [dbo].[Salevolume]
values
('112H',30),
('113H',40),
('122G',30),
('134F',50),
('118',100)
You need to fist check if item's last charater is alphabet or not. if it is a alphabet then check whether numeric value and alphabet value of item is between that of numeric value and alphabet value of itemfrom and itemto respectively as shown below:
select p.Saleperson,sum(v.Salevolume)
from Saleperson p
inner join Salevolume v
on v.Item between p.itemfrom and p.itemto
OR (
RIGHT(v.Item,1) between 'A' and 'Z'
and RIGHT(v.Item,1) between RIGHT(p.itemfrom,1) AND RIGHT(p.itemto,1)
and REPLACE(v.item,RIGHT(v.Item,1),'') between REPLACE(p.itemfrom,RIGHT(p.itemfrom,1),'') and REPLACE(p.itemto,RIGHT(p.itemto,1),'')
)
group by p.Saleperson;

ON DELETE CASCADE is not working is in sqlite3 in ios

I created a .sqlite file in ios programatically like below by enabling pragma foreignkeys ON
NSFileManager *theFileManager = [NSFileManager defaultManager];
if ([theFileManager fileExistsAtPath:[self getDatabasePath]] == NO)
{
char *theError;
const char *databasePath = [[self getDatabasePath] UTF8String];
const char *enableForienKey = [#"PRAGMA foreign_keys = ON;" UTF8String];
if (sqlite3_open(databasePath, &mDatabase) == SQLITE_OK)
{
if (sqlite3_exec(mDatabase, enableForienKey, NULL, NULL, &theError)!=SQLITE_OK)
{
DEBUGLOG(kCreateTableError,sqlite3_errmsg(mDatabase));
}
sqlite3_close(mDatabase);
}
else {
DEBUGLOG(KFailedToCreateDBFile);
}
}
Pragma foreign key is enabling but, I created two tables like below with create queries including ON DELETE CASCADE
// First table Create query
#"CREATE TABLE IF NOT EXISTS Session (sessionAppID INTEGER PRIMARY KEY NOT NULL , sessionID VARCHAR(255) NOT NULL, userAppID INTEGER, deviceAppID INTEGER NOT NULL, sessionStartTime VARCHAR(255) NOT NULL, sessionEndTime VARCHAR(255), sessionCreatedDateTime VARCHAR(200) NOT NULL,sessionUpdatedDateTime VARCHAR(200) NOT NULL, sessionFailureCount INTEGER NOT NULL,sessionStatus INTEGER NOT NULL, FOREIGN KEY(userAppID) REFERENCES User(userAppID), FOREIGN KEY(deviceAppID) REFERENCES Device(deviceAppID))"
//Second table which is child of first table query
#"CREATE TABLE IF NOT EXISTS EventLog (eventLogAppID INTEGER PRIMARY KEY NOT NULL , eventGUID VARCHAR(255) NOT NULL, sessionAppID INTEGER NOT NULL , eventName VARCHAR(255) NOT NULL, eventGroupGUID VARCHAR(255), eventParentGUID VARCHAR(255), eventCategory INTEGER NOT NULL,eventStartTime VARCHAR(255) NOT NULL, eventEndTime VARCHAR(255) ,eventDuration VARCHAR(255),eventType INTEGER NOT NULL,eventCreatedDateTime VARCHAR(200) NOT NULL,eventUpdatedDateTime VARCHAR(200) NOT NULL,eventFailureCount INTEGER NOT NULL,eventStatus INTEGER NOT NULL, FOREIGN KEY(sessionAppID) REFERENCES Session(sessionAppID)ON DELETE CASCADE)"
On deleting the session record, Only session record is deleting eventLog records are not deleting, Can any one please help on this, what will be the problem.By the way I am using sqlite3 version 3.7.1
See http://www.sqlite.org/foreignkeys.html#fk_enable. Note that you need to enable foreign keys for each connection.
Presumably you are creating a new connection to perform the delete query. Update your code so every time you open a db connection, you set the pragma as needed.
I was using singleton instance of SQLiteAsyncConnection in Xamarin.Forms, but anyways had to call "PRAGMA foreign_keys=ON;" before each DeleteAsync method call. Calling it once after opening the connection didn't work for me. To sum up what #rmaddy and #Karthik Mitta tried to jointly say

error 1452: Foreign Key error

I am new to mysql and I am trying create a gradebook db to keep track of grades for a certain class. I am using Mysql workbench and here is my code:
SET #OLD_UNIQUE_CHECKS=##UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET #OLD_FOREIGN_KEY_CHECKS=##FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET #OLD_SQL_MODE=##SQL_MODE, SQL_MODE='TRADITIONAL';
DROP SCHEMA IF EXISTS nj1368843 ;
CREATE SCHEMA IF NOT EXISTS nj1368843 DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ;
USE nj1368843 ;
-- Table nj1368843.Users
DROP TABLE IF EXISTS nj1368843.Users ;
CREATE TABLE IF NOT EXISTS nj1368843.Users (
idUsers INT NOT NULL AUTO_INCREMENT ,
UserName VARCHAR(45) NOT NULL ,
pw VARCHAR(45) NOT NULL ,
PRIMARY KEY (idUsers, UserName, pw) )
ENGINE = InnoDB;
INSERT INTO nj1368843.Users (UserName, pw) VALUES ('njack2', '123');
-- Table nj1368843.Teachers
DROP TABLE IF EXISTS nj1368843.Teachers ;
CREATE TABLE IF NOT EXISTS nj1368843.Teachers (
idTeachers INT NOT NULL ,
Lname VARCHAR(45) NULL ,
Fname VARCHAR(45) NULL ,
Users_idUsers INT NOT NULL ,
Users_pw VARCHAR(45) NOT NULL ,
PRIMARY KEY (idTeachers) ,
INDEX fk_Teachers_Users1 (Users_idUsers ASC, Users_pw ASC) ,
CONSTRAINT fk_Teachers_Users1
FOREIGN KEY (`Users_idUsers` , `Users_pw` )
REFERENCES `nj1368843`.`Users` (`idUsers` , `UserName` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
INSERT INTO nj1368843.Teachers (idTeachers, Lname, Fname, Users_idUsers, Users_pw) VALUES (105, 'Stacey', 'Sheila', '1', '123');
-- Table nj1368843.Schedule
DROP TABLE IF EXISTS nj1368843.Schedule ;
CREATE TABLE IF NOT EXISTS nj1368843.Schedule (
course_id INT NOT NULL ,
Semester VARCHAR(45) NULL ,
Year YEAR NULL ,
Teachers_idTeachers INT NOT NULL ,
PRIMARY KEY (course_id) ,
INDEX fk_Grades_Teachers1 (Teachers_idTeachers ASC) ,
CONSTRAINT fk_Grades_Teachers1
FOREIGN KEY (`Teachers_idTeachers` )
REFERENCES `nj1368843`.`Teachers` (`idTeachers` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- Table nj1368843.Assignments
DROP TABLE IF EXISTS nj1368843.Assignments ;
CREATE TABLE IF NOT EXISTS nj1368843.Assignments (
idAssignments INT NOT NULL ,
Assignment 1 INT NULL ,
AVG_Grade INT(11) NULL ,
Schedule_course_id INT NOT NULL ,
PRIMARY KEY (idAssignments) ,
INDEX fk_Assignments_Schedule1 (Schedule_course_id ASC) ,
CONSTRAINT fk_Assignments_Schedule1
FOREIGN KEY (`Schedule_course_id` )
REFERENCES `nj1368843`.`Schedule` (`course_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- Table nj1368843.Student
DROP TABLE IF EXISTS nj1368843.Student ;
CREATE TABLE IF NOT EXISTS nj1368843.Student (
idStudent INT NOT NULL ,
lname VARCHAR(45) NULL ,
fname VARCHAR(45) NULL ,
Schedule_course_id INT NOT NULL ,
Users_idUsers INT NOT NULL ,
Users_pw VARCHAR(45) NOT NULL ,
Assignments_idAssignments INT NOT NULL ,
PRIMARY KEY (idStudent) ,
INDEX fk_Student_Schedule1 (Schedule_course_id ASC) ,
INDEX fk_Student_Users1 (Users_idUsers ASC, Users_pw ASC) ,
INDEX fk_Student_Assignments1 (Assignments_idAssignments ASC) ,
CONSTRAINT fk_Student_Schedule1
FOREIGN KEY (`Schedule_course_id` )
REFERENCES `nj1368843`.`Schedule` (`course_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT fk_Student_Users1
FOREIGN KEY (`Users_idUsers` , `Users_pw` )
REFERENCES `nj1368843`.`Users` (`idUsers` , `UserName` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT fk_Student_Assignments1
FOREIGN KEY (`Assignments_idAssignments` )
REFERENCES `nj1368843`.`Assignments` (`idAssignments` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- Table nj1368843.Classes
DROP TABLE IF EXISTS nj1368843.Classes ;
CREATE TABLE IF NOT EXISTS nj1368843.Classes (
cid INT NOT NULL ,
Name VARCHAR(45) NULL ,
Schedule_course_id INT NOT NULL ,
PRIMARY KEY (cid) ,
INDEX fk_Classes_Schedule1 (Schedule_course_id ASC) ,
CONSTRAINT fk_Classes_Schedule1
FOREIGN KEY (`Schedule_course_id` )
REFERENCES `nj1368843`.`Schedule` (`course_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- Table nj1368843.Teachers
DROP TABLE IF EXISTS nj1368843.Teachers ;
CREATE TABLE IF NOT EXISTS nj1368843.Teachers (
idTeachers INT NOT NULL ,
Lname VARCHAR(45) NULL ,
Fname VARCHAR(45) NULL ,
Users_idUsers INT NOT NULL ,
Users_pw VARCHAR(45) NOT NULL ,
PRIMARY KEY (idTeachers) ,
INDEX fk_Teachers_Users1 (Users_idUsers ASC, Users_pw ASC) ,
CONSTRAINT fk_Teachers_Users1
FOREIGN KEY (`Users_idUsers` , `Users_pw` )
REFERENCES `nj1368843`.`Users` (`idUsers` , `UserName` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SET SQL_MODE=#OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=#OLD_UNIQUE_CHECKS;
I generated this out of an erd diagram and I can't insert any information in the database because I get:
ERROR 1452: Cannot add or update a child row: a foreign key constraint fails (nj1368843.Teachers, CONSTRAINT fk_Teachers_Users1 FOREIGN KEY (Users_idUsers, Users_pw) REFERENCES Users (idUsers, UserName) ON DELETE NO ACTION ON UPDATE NO ACTION)
SQL Statement:
INSERT INTO nj1368843.Teachers (idTeachers, Lname, Fname, Users_idUsers, Users_pw) VALUES (105, 'Stacey', 'Sheila', 1, '123')
I tried everyone's ideas and looked over the code a million times and still can't find the problem. I can't insert into none of the tables for this db.help.

iOS SQLite: How to automatically set foreign key?

My app uses SQLite, and I've sorted out the create table statements. The idea is that table A and B have a one-to-many (or one) relationship, so the foreign key will be in Table B. Now I know about autoincrement for creating the primary key but how will this work for the foreign key? What if I add one row for Table A and 5 rows for Table B (all of which ideally are linked to that single row in Table A)? Won't it just autoincrement from 001-005 in table B?
Yes, if it's one-to-many between A and B, and as you add records in B, you will auto increment B's primary key, but not the foreign key to A (assuming you make it a plain old INTEGER with no AUTOINCREMENT). Given your example, yes, B will have five records, 1-5, which will all point to record 1 in A. So, when you add the record in A, you grab its id via FMDB's lastInsertRowId (or sqlite's sqlite3_last_insert_rowid()), store that in a variable, and then use that when populating the foreign key in B. So, in short, you don't "automatically" set the foreign key, but just do so manually, but it isn't hard.
In terms of how to configure the tables, maybe it helps if we look at an example of a one to many relationships between authors and books (ignoring the possibility of co-authorship, but focusing on the fact that one author can write multiple books). Thus, you have two entities, a author (A) entity, and a book (B) entity. The book.book_author_id is a foreign key referencing author.author_id.
Thus, would look like:
CREATE TABLE author
(
author_id INTEGER PRIMARY KEY AUTOINCREMENT,
author_last_name TEXT,
author_first_name TEXT
);
CREATE TABLE book
(
book_id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT,
book_author_id INTEGER,
FOREIGN KEY (book_author_id) REFERENCES author (author_id)
);
INSERT INTO author (author_last_name, author_first_name) VALUES ('William', 'Shakespeare');
INSERT INTO book (title, book_author_id) VALUES ('Hamlet', 1);
INSERT INTO book (title, book_author_id) VALUES ('Macbeth', 1);
INSERT INTO book (title, book_author_id) VALUES ('Othello', 1);
INSERT INTO book (title, book_author_id) VALUES ('King Lear', 1);
INSERT INTO book (title, book_author_id) VALUES ('Henry V', 1);
And if we look at the results, it looks like:
$ sqlite3 test.db
SQLite version 3.7.12 2012-04-03 19:43:07
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .mode column
sqlite> .headers on
sqlite>
sqlite> CREATE TABLE author
...> (
...> author_id INTEGER PRIMARY KEY AUTOINCREMENT,
...> author_last_name TEXT,
...> author_first_name TEXT
...> );
sqlite>
sqlite> CREATE TABLE book
...> (
...> book_id INTEGER PRIMARY KEY AUTOINCREMENT,
...> title TEXT,
...> book_author_id INTEGER,
...> FOREIGN KEY (book_author_id) REFERENCES author (author_id)
...> );
sqlite>
sqlite> INSERT INTO author (author_last_name, author_first_name) VALUES ('William', 'Shakespeare');
sqlite>
sqlite> SELECT * FROM author;
author_id author_last_name author_first_name
---------- ---------------- -----------------
1 William Shakespeare
sqlite>
sqlite> INSERT INTO book (title, book_author_id) VALUES ('Hamlet', 1);
sqlite> INSERT INTO book (title, book_author_id) VALUES ('Macbeth', 1);
sqlite> INSERT INTO book (title, book_author_id) VALUES ('Othello', 1);
sqlite> INSERT INTO book (title, book_author_id) VALUES ('King Lear', 1);
sqlite> INSERT INTO book (title, book_author_id) VALUES ('Henry V', 1);
sqlite>
sqlite> SELECT * FROM book;
book_id title book_author_id
---------- ---------- --------------
1 Hamlet 1
2 Macbeth 1
3 Othello 1
4 King Lear 1
5 Henry V 1
sqlite> .quit
Or, if you want to do it programmatically (I'm using FMDB which makes this much simpler, but clearly the same logic works if you're doing your own sqlite3 calls, but it just takes a lot more code):
- (void)createAuthorTable
{
BOOL result = [_db executeUpdate:
#"CREATE TABLE IF NOT EXISTS author "
"("
"author_id INTEGER PRIMARY KEY AUTOINCREMENT, "
"author_last_name TEXT, "
"author_first_name TEXT "
");"];
NSAssert(result, #"%s - Unable to create author table", __FUNCTION__);
}
- (void)createBookTable
{
BOOL result = [_db executeUpdate:
#"CREATE TABLE IF NOT EXISTS book "
"("
"book_id INTEGER PRIMARY KEY AUTOINCREMENT, "
"title TEXT, "
"book_author_id INTEGER, "
"FOREIGN KEY (book_author_id) REFERENCES author (author_id) "
");"];
NSAssert(result, #"%s - Unable to create book table", __FUNCTION__);
}
- (sqlite_int64)createAuthorWithFirstName:(NSString *)firstName lastName:(NSString *)lastName
{
BOOL result = [_db executeUpdate:#"INSERT INTO author (author_first_name, author_last_name) VALUES (?, ?)", firstName, lastName];
NSAssert(result, #"%s - Unable to insert author record", __FUNCTION__);
return [_db lastInsertRowId];
}
- (sqlite_int64)createBookWithTitle:(NSString *)title authorId:(sqlite_int64)authorId
{
BOOL result = [_db executeUpdate:#"INSERT INTO book (title, book_author_id) VALUES (?, ?)", title, [NSNumber numberWithInt:authorId]];
NSAssert(result, #"%s - Unable to insert book record", __FUNCTION__);
return [_db lastInsertRowId];
}
- (void)testInsert
{
[self openDatabase];
NSArray *bookTitles = [NSArray arrayWithObjects:#"Hamlet", #"Macbeth", #"Othello", #"King Lear", #"Henry V", nil];
[self createAuthorTable];
[self createBookTable];
sqlite_int64 authorId = [self createAuthorWithFirstName:#"William" lastName:#"Shakespeare"];
sqlite_int64 bookId;
for (NSString *bookTitle in bookTitles)
bookId = [self createBookWithTitle:bookTitle authorId:authorId];
[self closeDatabase];
}
But before you can set the foreign keys, you have to turn it ON first. Do this first instead,
PRAGMA foreign_keys=ON;
A column with a foreign key constraint is not typically an autoincrementing value but refers to a (pre-existing) key value in another table. So if your AUTHORS table has an autoincrementing integer primary key, your TITLES.AuthorID column would simply be an integer with no autoincrement capability.
BTW, integer values do not have leading zeroes: 001-005 -- that usually implies a zero-padding which must be represented with a text/varchar datatype.

Elegant linq solution for left joins with unique data

I woud like to inquire if my Linq solution below is a good solution or if there is a better way. I am new to using Linq, and am most familiar with MySQL. So I've been converting one of my past projects from PHP to .NET MVC and am trying to learn Linq. I would like to find out if there is a better solution than the one I came up with.
I have the following table structures:
CREATE TABLE maplocations (
ID int NOT NULL AUTO_INCREMENT,
name varchar(35) NOT NULL,
Lat double NOT NULL,
Lng double NOT NULL,
PRIMARY KEY (ID),
UNIQUE KEY name (name)
);
CREATE TABLE reservations (
ID INT NOT NULL AUTO_INCREMENT,
loc_ID INT NOT NULL,
resDate DATE NOT NULL,
user_ID INT NOT NULL,
PRIMARY KEY (ID),
UNIQUE KEY one_per (loc_ID, resDate),
FOREIGN KEY (user_ID) REFERENCES Users (ID),
FOREIGN KEY (loc_ID) REFERENCES MapLocations (ID)
);
CREATE TABLE Users (
ID INT NOT NULL AUTO_INCREMENT,
name VARCHAR(20) NOT NULL,
email VARCHAR(50) NOT NULL,
pass VARCHAR(128) NOT NULL,
salt VARCHAR(5) NOT NULL,
PRIMARY KEY (ID),
UNIQUE KEY unique_names (name),
UNIQUE KEY unique_email (email)
);
In MySQL, I use the following query to get the ealiest reservation at each maplocation with a non null date for any locations that don't have a reservation.
SELECT locs.*, if(res.resDate,res.resDate,'0001-01-01') as resDate, res.Name as User
FROM MapLocations locs
LEFT JOIN (
SELECT loc_ID, resDate, Name
FROM Reservations, Users
WHERE resDate >= Date(Now())
AND user_ID = Users.ID
ORDER BY resDate
) res on locs.ID = res.loc_ID
group by locs.ID
ORDER BY locs.Name;
In Linq, with Visual studio automatically creating much of the structure after connecting to the database, I have come up with the following equivalent to that SQL Query
var resList = (from res in Reservations
where res.ResDate >= DateTime.Today
select res);
var locAndRes =
(from loc in Maplocations
join res in resList on loc.ID equals res.Loc_ID into join1
from res2 in join1.DefaultIfEmpty()
join usr in Users on res2.User_ID equals usr.ID into join2
from usr2 in join2.DefaultIfEmpty()
orderby loc.ID,res2.ResDate
select new {
ID = (int)loc.ID,
Name = (string)loc.Name,
Lat = (double)loc.Lat,
Lng = (double)loc.Lng,
resDate = res2 != null ?(DateTime)res2.ResDate : DateTime.MinValue,
user = usr2 != null ? usr2.Name : null
}).GroupBy(a => a.ID).Select(b => b.FirstOrDefault());
So, I'm wondering is there a better way to perform this query?
Are these equivalent?
Are there any good practices I should be following?
Also, one more question, I'm having trouble getting this from the var to a List. doing something like this doesn't work
List<locAndResModel> locList = locAndRes.AsQueryable().ToList<locAndResModel>();
In the above snippet locAndResModel is just a class which has variables to match the int, string, double double, DateTime, string results of the query. Is there an easy way to get a list without having to do a foreach and passing the results to a constructor override? Or should I just add it to ViewData and return the View?
You'll want to take advantage of the automatic joins performed by the Entity Framework. Give this a try and let me know if it does what you want:
var locAndRes = from maplocation in MapLocations
let earliestReservationDate = maplocation.Reservations.Min(res => res.resDate)
let earliestReservation = (from reservation in mapLocation.Reservations
where reservation.resDate == earliestReservationDate && reservation.resDate >= DateTime.Today
select reservation).FirstOrDefault()
select new locAndResModel( maplocation.ID, maplocation.name, maplocation.Lat, maplocation.Lng, earliestReservation != null ? earliestReservation.resDate : DateTime.MinValue, earliestReservation != null ?earliestReservation.User.name : null)

Resources