var query = (from student in dataset.Students
where student.subjectId == SubjectId || student.subjectId ==dataset.Subjects.FindBySubjecttId(SubjectId).PrimarySubjectId
select student)
The above Linq to SQL query fails if PrimarySubjectId is null.
PrimarySubjectId can have null values in the database. If there is no record for the subjectId I want the PrimarySubjectId which can be null. How do i handle the null values for
PrimarySubjectId ?
var query = (from student in dataset.Students
where student.subjectId == SubjectId ||
student.subjectId==dataset.Subjects.FindBySubjecttId(SubjectId).PrimarySubjectId
select student)
Since your PrimarySubjectId allows null so as per your linq statement, the property student.subjectid must be allowed to accept null values by making it Nullable subjectId or int? subjectid.
Related
I am having the following issue:
When I execute the following statement, I get an error for it returning more than one row
INSERT INTO artist
(personid,
musicgenreid,
totallikes)
VALUES ( (SELECT personid
FROM person
WHERE firstname = 'Joe'
AND middlename = ''
AND lastname = 'blow'),
(SELECT musicgenreid
FROM musicgenre
WHERE musicgenreid = 4),
( totallikes = 328374 ) );
I am getting the error on the (select pesonID from person...) statement, and I am trying to use the 'any' keyword to just grab any row, but I cannot seem to get it to work. I have tried just about any permutation I can think of of the current query and 'any', but it does not work. Is there another solution I should be trying or am I just missing the mark for some reason?
It seems you're trying to do something like this:
INSERT INTO artist (personid, musicgenreid, totallikes)
VALUES (
(SELECT personid FROM person
WHERE firstname = 'Joe' AND middlename = '' AND lastname = 'blow'
ORDER BY RAND()
LIMIT 1
),
4,
328374
);
This will get you a random personid that matches the given criteria.
The musicgenreid in your query would be either null or 4. I am forcing it to 4 as it seems that you're manually adding them and you know they already exist.
The total likes field is also fixed but your syntax was incorrect.
try with this sql statement
INSERT INTO artist
(personid,
musicgenreid,
totallikes)
VALUES ( (SELECT personid
FROM person
WHERE firstname = 'Joe'
AND middlename = ''
AND lastname = 'blow' LIMIT 1 ),
4,
328374);
How can I count the records in a MySQL table in Hibernate? I tried the following HQL, but it does not work.
SELECT COUNT(*) FROM MEMBERS WHERE `username` =:USERNAME OR `email` =:EMAIL
It is used in the following method:
public boolean checkInfos() {
Session newSession = NewHibernateUtil.getSessionFactory().openSession();
int count = (Integer) newSession.createSQLQuery("SELECT COUNT(*) FROM MEMBERS WHERE `username` ='admin' OR `email` ='admin'").uniqueResult();
if (count >= 1) {
return false;
} else {
return true;
}
}
I guess you need that
Query q = newSession.createSQLQuery("SELECT COUNT(*) FROM MEMBERS WHERE username = ? OR email =?");
q.setParameter( 1, "your username");
q.setParameter(2, "your email");
Select count(*) returns a Long, not an Integer.
One more thing: The backticks, which you are using, are not accepted by every database.
By the way, you can use count(*) also in HQL. This means, you can use createQuery instead of createSQLQuery. An advantage of HQL is, it is portable from one database to another, which is not always the case for SQL statements.
You also can achieve this by using criteria also.
private Number getCount(){
Criteria criteria = session.createCriteria(Members.class);
criteria.add(Restrictions.or(Restrictions.eq("username", "admin"), Restrictions.eq("email","admin")));
criteria.setProjection(Projections.rowCount());
return (Number) criteria.list();
}
I need to get the details from the table using LINQ in C# and the where condition is ID and here my condition is ,if any of the column in the selected row is empty or null i need to return as False if not means True should be returned.....Cn any one help for this...Thanks in advance.
Suppose your table has the following columns: ID, Col1, Col2, Col3, Col4. You can the use the following Linq statement.
bool anyIsNull = (from row in context.Table
where row.ID = id
select (row.Col1 == null || row.Col2 == null ||
row.Col3 == null || row.Col4 == null).Single();
I have table called Customers and this customer has Bank Details
but Some customer they don't have,
When i Use Linq to sql it's Return Null value, like a below example Table
How to prevent this null into Default value 0 or String
Ex. Table
Customer ID Name bank name
----------- ------ --------------
J0002 John HSBC
K0001 Kethy SMC
L0003 Mike Null
S0004 Lilie Null
Thanks
Something like
var customers = (from c in Customers
select new Customer
(
c.CustomerId,
c.Name,
c.BankName ?? ""
)).ToList();
or
var result = Customer.Select(x => new
{
CustomerId = x.CustomerId,
Name = x.Name,
BankName = x.BankName.HasValue ? x.BankName : ""
}).OrderBy(p => p.Name).ToArray();
could do the trick. It would be easier if you showed your current query.
I have a table with a column that has null values... when I try to query for records where that column IS NULL:
THIS WORKS:
var list = from mt in db.MY_TABLE
where mt.PARENT_KEY == null
select new { mt.NAME };
THIS DOES NOT:
int? id = null;
var list = from mt in db.MY_TABLE
where mt.PARENT_KEY == id
select new { mt.NAME };
Why?
after some more googling, I found the answer:
ref #1
ref #2
int? id = null;
var list = from mt in db.MY_TABLE
where object.Equals(mt.PARENT_KEY, id) //use object.Equals for nullable field
select new { mt.NAME };
This LINQ renders to SQL as follows:
((mt.PARENT_KEY IS NULL) AND (#id IS NULL))
OR ((mt.PARENT_KEY IS NOT NULL) AND (#id IS NOT NULL) AND (mt.PARENT_KEY = #id))
One possibility - if mt.PARENT_KEY is of some other type (e.g. long?) then there will be conversions involved.
It would help if you could show the types involved and the query generated in each case.
EDIT: I think I have an idea...
It could be because SQL and C# have different ideas of what equality means when it comes to null. Try this:
where (mt.PARENT_KEY == id) || (mt.PARENT_KEY == null && id == null)
If this is the case then it's a pretty ugly corner case, but I can understand why it's done that way... if the generated SQL is just using
WHERE PARENT_KEY = #value
then that won't work when value is null - it needs:
WHERE (PARENT_KEY = #value) OR (PARENT_KEY IS NULL AND #value IS NULL)
which is what the latter LINQ query should generate.
Out of interest, why are you selecting with
select new { mt.NAME }
instead of just
select mt.NAME
?) Why would you want a sequence of anonymous types instead of a sequence of strings (or whatever type NAME is?
It's definitely a matter of C# and SQL having different notions of how to compare nulls - the question has been addressed here before:
Compare nullable types in Linq to Sql