I need to get the id value from Table1 which is a Guid. This query can return a null also. So I started with following
Guid? SomeID = from R in Table1
join P in Table2
on R.Id equals P.Id2
where R.Name == 'blah blah'
select R.Id;
But I get following compilation error.
Cannot implicitly convert type 'System.Linq.IQueryable'
to 'System.Guid?'
Changing Guid? to Guid didn't help.
Guid SomeID = from R in Table1
join P in Table2
on R.Id equals P.Id2
where R.Name == 'blah blah'
select R.Id;
as now I get following error.
Cannot implicitly convert type 'System.Linq.IQueryable'
to 'System.Guid'
What am I doing wrong?
Your query returns all matching guids and it has type IQueryable<Guid?>
IQueryable<Guid?> guids =
from R in Table1
join P in Table2
on R.Id equals P.Id2
where R.Name == 'blah blah'
select R.Id;
If you need one guid, use First, Single, FirstOrDefault, or SingleOrDefault
Guild? guid = guids.FirstOrDefault();
Or in single statement:
Guid? guid = Table1.Where(R => R.Name == "blah")
.Join(Table2, R => R.Id, P => P.Id2, (R,P) => R.Id)
.FirstOrDefault();
Mixed syntax (unfortunately there is no equivalent for FirstOrDefault operator in query syntax):
Guid? guid = (from R in Table1
join P in Table2
on R.Id equals P.Id2
where R.Name == 'blah blah'
select R.Id).FirstOrDefault();
Enumerable.Select returns an IEnumerable<T> which is a sequence, so normally multiple items. If you want the first item use First or (if it can be empty) FirstOrDefault:
Guid? SomeID = (from R in Table1
join P in Table2
on R.Id equals P.Id2
where R.Name == 'blah blah'
select R.Id).First();
The return value of that LINQ statement is a list of GUIDs. Specifically, an IEnumerable<Guid>.
Call .First() on the result of your query if you're just expecting one result, or .FirstOrDefault() if you might end up with no results and you just want to receive a null.
(from R in Table1
join P in Table2
on R.Id equals P.Id2
where R.Name == 'blah blah'
select R.Id).FirstOrDefault() //or .SingleOrDefault()
IQueryable represents query for some kind of store (e.g. SQL Server) and can potentially return 0,1 or more rows. You are trying to get one of them. To get single row use methods like First/FirstOrDefault or Single/SingleOrDefult
You need to use Single or SingleOrDefault or First, or FirstOrDefault method. They exist only in lambda syntax. If you expect one result use Single, if one or zero, then SingleOrDefault.
Try this code
use as Guid
Look below :
Guid SomeID = (from R in Table1
join P in Table2
on R.Id equals P.Id2
where R.Name == 'blah blah'
select R.Id).First() as Guid;
or try it
Guid SomeID = (Guid) from R in Table1
join P in Table2
on R.Id equals P.Id2
where R.Name == 'blah blah'
select R.Id).First();
Related
I make some joins to access the field IsVacancyActive.
When I write condition s.IsVacancyActive == null, I'm getting several rows and there is one which shouldn't be there.
And still if I write s.IsVacancyActive != null, then I'm geting another collection of rows, including that one row, which I have in the previous condition. How can it be? How it can have both values null and not null?
P.S. I use into for the left join
var data = (from c in db.Table1
join cm in db.Table2 on c.CandidateId equals cm.CandidateId into var1 from cm in var1.DefaultIfEmpty()
join lp in db.Table3 on c.KotelPositionId equals lp.PositionId into var2 from lp in var2.DefaultIfEmpty()
join pg in db.Table4 on lp.PositionId equals pg.PositionId into var3 from pg in var3.DefaultIfEmpty()
join lpt in db.Table5 on pg.LinkPositionGradeId equals lpt.LinkPositionGradeId into var4 from lpt in var4.DefaultIfEmpty()
join s in db.Table6 on lpt.LinkPositionTeamId equals s.LinkPositionTeamId into var5 from s in var5.DefaultIfEmpty()
join cv in db.Table7 on s.StaffId equals cv.VacancyId into var6 from cv in var6.DefaultIfEmpty()
join cvac in db.Table8 on c.CandidateId equals cvac.CandidateId into var7 from cvac in var7.DefaultIfEmpty()
where s.IsVacancyActive == null// Error
select new
{
c
}).Distinct()
.ToList();
If the LINQ query in the question ultimately resolves to a SQL database transaction, then the where clause:
where s.IsVacancyActive == null// Error
...may be parsed by LINQ to a null comparison in SQL, i.e. as X = NULL or X != NULL, which are both false in SQL.
Try:
where s.IsVacancyActive.HasValue
The concat works, but the outer distinct query does not. The error is 'string does not contain a definition for 'role_name'... Any idea how to make this work?
(from results in ((from pmr in dbContext.project_member_role
join r in dbContext.role on pmr.project_member_role_id equals r.role_id
where pmr.member_id == memberId
select r.role_name)
.Concat(from m in dbContext.member
join r in dbContext.role on m.portal_role_id equals r.role_id
where m.member_id == memberId
select r.role_name))
select results.role_name).Distinct().ToList();
I'm trying to select the MatchingObjects that doesn't exist in the Unlock table , I have this SQL query as following:
select a.* from MatchingObjects a
left join Unlocks b
on a.ObjectCategoryId = b.ObjectCategoryId
left join Members c
on b.StudentId = c.Id
and b.StudentId = #studentId
where b.ObjectCategoryId is null
and c.id is null
order by a.ObjectCategoryId
And a LINQ query
var query = (from d in db.ObjectCategories
join a in db.MatchingObjects on d.Id equals a.ObjectCategoryId into grp3
join b in db.Unlocks
on d.Id equals b.ObjectCategoryId into grp1
from m in grp1.DefaultIfEmpty()
join c in db.Members
on m.StudentId equals c.Id into grp2
from n in grp2.DefaultIfEmpty()
where m.ObjectCategoryId == null
&& n.Id == null
orderby d.Id).AsEnumerable()
;
However, the LINQ query is not showing the same result as that I want like in the SQL query. Could you guys tell me what I should change in my LINQ Query?
This is the model:
Better you can use below tools:
An SQL-> LINQ converter..
http://www.sqltolinq.com
http://www.linqpad.net/
try this one
var query = from m in db.MatchingObjects.Where(w => w.ObjectCategoryId == null)
join u in db.Unlocks.Where(w => w.studentId == #studentId)
on m.ObjectCategoryId equals u.ObjectCategoryId into joinedU
from ju in joinedU.DefaultIfEmpty()
join m in db.Members.Where(w => w.id == null)
on ju.StudentId equals m.Id into joinedM
from jm in joinedM.DefaultIfEmpty()
select m;
But your request is a bit Strange. You make the join by ObjectCategoryId and in the Where clause you put ObjectCategoryId == null!!!
sorry guys, the Sql and the LINQ query was a bit different in table selection. I'm sorry about that because I was trying different Linq while posting the question, but The main problem is at the join clause
(from d in db.ObjectCategories
join a in db.MatchingObjects
on d.Id equals a.ObjectCategoryId into grp3
join b in db.Unlocks
on d.Id equals b.ObjectCategoryId into grp1
from m in grp1.DefaultIfEmpty()
join c in db.Members
on m.StudentId equals studentId into grp2
from n in grp2.DefaultIfEmpty()
where m.ObjectCategoryId == null
where n.Id == null
orderby d.Id).AsEnumerable()
/* this is the correct one */
join c in db.Members
on m.StudentId equals studentId into grp2
/* the below was the original incorrect join clause*/
join c in db.Members
on m.StudentId equals c.Id into grp2
I have the following SQL statement:
SELECT r.NAME AS REGIONNAME, s.NAME AS STADTNAME, f.ADRESSE AS STRASSEADRESSE
FROM REGION r
LEFT OUTER JOIN STADT s ON s.REGION_ID = r.ID
LEFT OUTER JOIN GEBAEUDE f ON s.ID = f.STADT_ID
ORDER BY r.NAME
If I use this statement in SQL Server 2008 Management Studio, it works like I wanted.
The output is:
On the client-side I am working with Xcode. My problem is that I return REGIONNAME, STADTNAME and STRASSENADRESSE in a TableView. If the STRASSENADRESSE value in the database is NULL, Xcode returns an empty row.
Is it possible, if the STRASSENADRESSE Value is NULL, that the web service only returns the REGIONNAME and STADTNAME? If yes how can I do that.
Thx in advance
this query will return 3 columns
SELECT
r.NAME AS REGIONNAME, s.NAME AS STADTNAME, f.ADRESSE AS STRASSEADRESSE
FROM REGION r
LEFT OUTER JOIN STADT s ON s.REGION_ID = r.ID
LEFT OUTER JOIN GEBAEUDE f ON s.ID = f.STADT_ID ORDER BY r.NAME
WHERE f.ADRESSE IS NOT NULL
this one will return only two values, when the STRASSEADRESSE value is null
SELECT
r.NAME AS REGIONNAME, s.NAME AS STADTNAME,
FROM REGION r
LEFT OUTER JOIN STADT s ON s.REGION_ID = r.ID
LEFT OUTER JOIN GEBAEUDE f ON s.ID = f.STADT_ID ORDER BY r.NAME
WHERE f.ADRESSE IS NULL
you will have to recieve them in two distinct TableView, one with 3 columns and another one with 2 columns
You can use the built in ISNULL() SQL function to return an empty string, or some other default value.
SELECT
r.NAME AS REGIONNAME,
s.NAME AS STADTNAME,
ISNULL(f.ADRESSE,'') AS STRASSEADRESSE
FROM
REGION r LEFT OUTER JOIN
STADT s ON s.REGION_ID = r.ID LEFT OUTER JOIN
GEBAEUDE f ON s.ID = f.STADT_ID
ORDER BY
r.NAME
You can check if 'ADRESSE' is NULL Or not..
You can use WHEN ans IS NULL functions..
SELECT
r.NAME AS REGIONNAME,
s.NAME AS STADTNAME,
CASE f.ADDRESS
WHEN f.ADRESSE IS NULL THEN ''
ELSE f.ADDRESS
END AS STRASSEADRESSE
FROM
REGION r LEFT OUTER JOIN
STADT s ON s.REGION_ID = r.ID LEFT OUTER JOIN
GEBAEUDE f ON s.ID = f.STADT_ID
ORDER BY
r.NAME
Check this links
http://msdn.microsoft.com/en-us/library/ms181765.aspx
For some reason I am getting a "Query body must end with a select clause or a group clause" compile error on what seems to be a simple compound conditional in the following linq-to-sql query:
using (var db = new CaremcDB(Database.Conn))
{
var taxids = from p in db.ProviderTaxIds
join c in db.CustomerProviders
on customerId equals c.CustomerId && p.Id equals c.ProviderId
select p;
return taxids.ToList<ProviderTaxIds>();
}
It chokes on the "&& p.Id equals c.ProviderId" clause for some reason.
It appears that customerId is an external input to the query. Move that to a where clause.
...
on p.Id equals c.ProviderId
where customerId == c.CustomerId
select p;
try this, the parameter names just need to match in the anonymous object
join c in db.CustomerProviders on new { customerId, p.Id } equals new { customerId = c.CustomerId, Id= c.ProviderId }