Below is my code using LINQ to SQL,on execution I get an error msg saying "Object reference not set to an instance of an object".I've joined 3 tables Users,UsersinRoles and Roles.Userinroles is the bridge table.I use join to retrive datas from 2 tables using bridge table i get the Object reference error.
public class Users : CollectionFactoryBase
{
public Users()
{
this.Summary = "Collection of Users";
}
public override Collection MakeCollection(CollectionRequestContext context)
{
UsersDataContext m_dataContext = new UsersDataContext();
const int maxItems_c = 150;
try
{
// string sessionvalue = HttpContext.Current.Session["SessionKey"] as string;
var Users = from p in m_dataContext.aspnet_Users
join t in m_dataContext.aspnet_UsersInRoles on
p.UserId equals t.UserId
join r in m_dataContext.aspnet_Roles on
t.RoleId equals r.RoleId
select new
{
UserName = p.UserName,
UserId = p.UserId,
RoleId = r.RoleId,
RoleName = r.RoleName,
userid = t.UserId,
roleid = t.RoleId
};
Collection collection = new Collection();
collection.Name = "Users";
foreach (var user in Users.Take(maxItems_c) )
{
collection.AddItem(user.UserName, user.RoleName, null, null, null, null, null);
}
return collection;
}
catch (Exception ex)
{
return ErrorCollection.FromException(ex);
}
}
}
}
Don't you think that last statement should be first one?
Related
My post request body will be like
{
"queryCondition":[
{
"filter":"status",
"filterlist":["Closed","New","Resolved"...]
},
{
"filter":"assigned_team",
"filterlist":["A","B","C"...]
},
{
"filter":"assigned_to",
"filterlist":["ram","govind","ajith"...]
},
{
"filter":"duration",
"filterlist":["2020-02-01","2020-05-01"....]
}
....
....
],
"durationField":"created_date"
}
I receive the columns(filter) and values(filterlist) dynamically with which I need to build this query.
SELECT * FROM tickets
WHERE ticket_id IN (SELECT ticket_id FROM Tickets WHERE created_date >= '2020-02-01') AND created_date '2020-05-01'
AND status IN ('Closed','Resolved','New')
AND assigned_team IN ('A' , 'B', 'C')
AND assigned_to IN ('ram','govind','ajith');
I built this query dynamically using Predicate and it is working fine.
#Override
public List<Tickets> conditionedQuery(QueryCondition queryCondition) {
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Tickets> query = cb.createQuery(Tickets.class);
Root<Tickets> ticket = query.from(Tickets.class);
List<Predicate> predicatessub = new ArrayList<>();
for(FilterConditions fc:queryCondition.getQueryCondition()) {
if(fc.getFilter().equals("duration")) {
Predicate ps = cb.greaterThanOrEqualTo(ticket.get(queryCondition.getDurationField()), fc.getFilterlist()[0]);
Predicate pe = cb.lessThan(ticket.get(queryCondition.getDurationField()), fc.getFilterlist()[1]);
predicatessub.add(cb.and(ps,pe));
}else
{
List<Predicate> predicates = new ArrayList<>();
for(int i=0; i<fc.getFilterlist().length; i++) {
Predicate p = cb.equal(ticket.get(fc.getFilter()),fc.getFilterlist()[i]);
predicates.add(p);
}
predicatessub.add(cb.or(predicates.toArray(new Predicate[predicates.size()])));
}
}
query.select(ticket)
.where(cb.and(predicatessub.toArray(new Predicate[predicatessub.size()])));
return entityManager.createQuery(query)
.getResultList();
}
QueryCondition.class
public class QueryCondition {
private List<FilterConditions> filterCondition;
private String durationField;
}
FilterConditions.class
public class FilterConditions {
private String filter;
private String[] filterlist;
}
Now I would like to build a quite more complex query involving joins and group by. Below is the sample query like what I would like to build using predicate.
SELECT
YEAR(pt.created_date),
MONTH(pt.created_date),
pt.assigned_team,
COUNT(tk.ticket_id)
FROM
(SELECT
*
FROM
tickets
WHERE
ticket_id IN (SELECT
ticket_id
FROM
Tickets
WHERE
resolved_date >= '2020-02-01')
AND resolved_date < '2020-05-01'
and assigned_team IN ('A' , 'B', 'C')) pt
LEFT JOIN
(SELECT
*
FROM
tickets
WHERE
status IN ('Closed','Resolved','New')
AND assigned_to IN ('ram','govind','ajith')) tk ON tk.ticket_id = pt.ticket_id
GROUP BY YEAR(pt.created_date) , MONTH(pt.created_date), pt.assigned_team order by
pt.assigned_team,YEAR(pt.created_date),MONTH(pt.created_date) asc;
Kindly advise how this can be achieved with Predicate or is there any other simpler way than Predicate.
Maybe this works:
First create a list with all field your model but removing associations. After pass this list in CriteriaQuery.
List<Expression<?>> groupByList = new ArrayList<>();
// ticket => Root<Tickets>
ticket.getModel().getAttibutes().stream()
.filter(a -> !a.isAssociation())
.forEach(a -> groupByList.add(ticket.get(a.getName())));
query.select(ticket)
.where(cb.and(predicatessub.toArray(new Predicate[predicatessub.size()])))
.groupBy(groupByList); // <== add group by
If you have join just do the same thing creating a cast with object EntityTypeImpl<?>. ex:
// import org.hibernate.metamodel.model.domain.internal.EntityTypeImpl
// join => Join<?,?>
((EntityTypeImpl<MyEntity>) join.getModel())
.getDeclaredAttributes().stream()
.filter(a -> !a.isAssociation())
.forEach(a -> groupByList.add(join.get(a.getName())));
I am trying to do an insert using linq to sql but am getting the following error
Additional information: Cannot insert the value NULL into column 'UserID', table 'Itiss_Request.dbo.Users'; column does not allow nulls. INSERT fails.
The UserID table is the pk aswel as the identity has been set to autoincrement.
The database has 4 fields.
DataClasses1DataContext dt = new DataClasses1DataContext();
User usr = new User();
usr.MudID = a[1];
usr.Email = Session["email"].ToString();
usr.Name = Session["userName"].ToString();
dt.Users.InsertOnSubmit(usr);
dt.SubmitChanges();
This is an from my context file
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_UserID", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
public int UserID
{
get
{
return this._UserID;
}
set
{
if ((this._UserID != value))
{
this.OnUserIDChanging(value);
this.SendPropertyChanging();
this._UserID = value;
this.SendPropertyChanged("UserID");
this.OnUserIDChanged();
}
}
}
Please try this...
DataClasses1DataContext dt = new DataClasses1DataContext();
User usr = new User();
usr.MudID = a[1];
usr.Email = Session["email"].ToString();
usr.Name = Session["userName"].ToString();
dt.Users.AddObject(usr);
dt.SaveChanges();
I created an example rest webservice using WebApi.
Here is the controller class named Employee Controller.
namespace HelloWebApi.Controllers
{
public class EmployeesController : ApiController
{
private static IList<Employee> list = new List<Employee>()
{
new Employee()
{
Id = 12345, FirstName = "John", LastName = "Human",Department = 2
},
new Employee()
{
Id = 12346, FirstName = "Jane", LastName = "Public",Department = 3
},
new Employee()
{
Id = 12347, FirstName = "Joseph", LastName = "Law",Department = 2
}
};
// GET api/employees
public IEnumerable<Employee> Get()
{
return list;
}
// GET api/employees/12345
public Employee Get(int id)
{
return list.First(e => e.Id == id);
}
// POST api/employees
public void Post(Employee employee)
{
int maxId = list.Max(e => e.Id);
employee.Id = maxId + 1;
list.Add(employee);
}
// PUT api/employees/12345
public void Put(int id, Employee employee)
{
int index = list.ToList().FindIndex(e => e.Id == id);
list[index] = employee;
}
// DELETE api/employees/12345
public void Delete(int id)
{
Employee employee = Get(id);
list.Remove(employee);
}
public HttpResponseMessage Patch(int id, Delta<Employee> deltaEmployee)
{
var employee = list.FirstOrDefault(e => e.Id == id);
if (employee == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
deltaEmployee.Patch(employee);
return Request.CreateResponse(HttpStatusCode.NoContent);
}
}
}
Here I used all HTTP verbs method for insert, update, delete. I used a list that stores the Employee objects. insertion, deletion and updation are based on this list. But this program has no database connection. I want to know how to use Database access in this program. I would like to use tables from a database. How could i do it? Any useful link please.
I'm trying to write a linq to sql method that handles sorting, paging, and filtering for an ajax grid. I created a partial class Employee that has a TotalRecordCount, as I need to pass this to the javascript for setting up the pager. The problem is that it won't build because I can't set the AnonymousType#1.TotalRecordCount, it's read-only. Yet if I do "select new Employee", then it will throw the Exception - "Explicit construction of entity type 'InVision.Data.Employee' in query is not allowed.".
Here's the code...
public string GetPageJSON(string sortColumn, string sortDirection, int pageNumber, int pageSize, EmployeeSearch search)
{
var query = from e in db.Employees
select new
{
EmployeeID = e.EmployeeID,
FirstName = e.FirstName,
LastName = e.LastName,
LoginName = e.LoginName,
IsLockedOut = e.IsLockedOut,
TotalRecordCount = e.TotalRecordCount
};
//searching.
if (search.FirstName.Length > 0) query = query.Where(e => e.FirstName.Contains(search.FirstName));
if (search.LastName.Length > 0) query = query.Where(e => e.LastName.Contains(search.LastName));
if (search.LoginName.Length > 0) query = query.Where(e => e.LoginName.Contains(search.LoginName));
if (search.Status.Length > 0) query = query.Where(e => (search.Status == "Active" && !e.IsLockedOut)
|| (search.Status == "Inactive" && e.IsLockedOut));
//sorting.
query = query.OrderBy(sortColumn, sortDirection);
//get total record count.
int totalRecordCount = query.Count();
//paging.
query = query.Skip((pageNumber - 1) * pageSize).Take(pageSize);
//set total record count.
var list = query.ToList();
if (list.Count > 0)
{
list[0].TotalRecordCount = totalRecordCount; //throws exception
}
//return json.
JavaScriptSerializer serializer = new JavaScriptSerializer();
return serializer.Serialize(list);
}
You'll want to select the original objects rather than mapping them to new objects (whether of the same type, or an anonymous type).
Replace this:
var query = from e in db.Employees
select new
{
EmployeeID = e.EmployeeID,
FirstName = e.FirstName,
LastName = e.LastName,
LoginName = e.LoginName,
IsLockedOut = e.IsLockedOut,
TotalRecordCount = e.TotalRecordCount
};
With this:
var query = db.Employees.AsQueryable();
Then later on replace this:
var list = query.ToList();
if (list.Count > 0)
{
list[0].TotalRecordCount = totalRecordCount;
}
With this:
var list = from e in query
select new
{
EmployeeID = e.EmployeeID,
FirstName = e.FirstName,
LastName = e.LastName,
LoginName = e.LoginName,
IsActive = !e.IsLockedOut,
TotalRecordCount = totalRecordCount
};
I think that should be everything. If the JavaScriptSerializer requires a List, just make sure you use it like this: return serializer.Serialize(list.ToList());
I ended up using a custom view class to get this to work...
partial class EmployeeView
{
public int EmployeeID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string LoginName { get; set; }
public bool IsActive { get; set; }
public int TotalRecordCount { get; set; }
}
public string GetPageJSON(string sortColumn, string sortDirection, int pageNumber, int pageSize, EmployeeSearch search)
{
var query = from e in db.Employees
select new EmployeeView
{
EmployeeID = e.EmployeeID,
FirstName = e.FirstName,
LastName = e.LastName,
LoginName = e.LoginName,
IsActive = !e.IsLockedOut,
TotalRecordCount = 0
};
//searching.
if (search.FirstName.Length > 0) query = query.Where(e => e.FirstName.Contains(search.FirstName));
if (search.LastName.Length > 0) query = query.Where(e => e.LastName.Contains(search.LastName));
if (search.LoginName.Length > 0) query = query.Where(e => e.LoginName.Contains(search.LoginName));
if (search.Status.Length > 0) query = query.Where(e => (search.Status == "Active" && e.IsActive)
|| (search.Status == "Inactive" && !e.IsActive));
//sorting.
query = query.OrderBy(sortColumn, sortDirection);
//get total record count.
int totalRecordCount = query.Count();
//paging.
query = query.Skip((pageNumber - 1) * pageSize).Take(pageSize);
//set total record count.
var list = query.ToList();
if (list.Count > 0)
{
list[0].TotalRecordCount = totalRecordCount;
}
//return json.
JavaScriptSerializer serializer = new JavaScriptSerializer();
return serializer.Serialize(list);
}
Imagine the following code:
return from a in DBContext.Acts
join artist in DBContext.Artists
on a.ArtistID equals artist.ID into art
from artist in art.DefaultIfEmpty()
select new Shared.DO.Act {
ID = a.ID,
Name = a.Name,
Artist = new Shared.DO.Artist {
ID = artist.ID,
Name = artist.Name
},
GigId = a.GigID
};
As you can see, the linqtosql generated act object is adapted into the Shared.DO.Act object. As part of this a linqtosql generated Artist object is adapted into a Shared.DO.Artist
Elsewhere in my code base, I may want to request an Artist (see below):
return from a in DBContext.Artists
select new Shared.DO.Artist {
ID = artist.ID,
Name = artist.Name
},
GigId = a.GigID
};
This means the adption code for an Artist now appears in two places! Once when getting an artist and also when an act is loaded
How should I centralise this adaption code?
I would do this using an Artists class with a dictionary of artists:
public class Artists
{
// key should be whatever type artist.ID is
private Dictionary<int, Shared.DO.Artist> _artistDictionary;
public static Get(int id)
{
if (_artistDictionary == null)
_artistDictionary = new Dictionary<int, Shared.DO.Artist>();
if (!_artistDictionary.ContainsKey(id))
{
var artist = from a in DBContext.Artists
on a.ID equals id
select new Shared.DO.Artist {
ID = a.ID,
Name = a.Name
};
_artistDictionary.Add(id, artist);
}
return _artistDictionary[id];
}
}