I'm trying to join two tables using their common id which is MOTHERID. But he code below was working fine but at some point it starts throwing "Invalid cast exception" at:
var firstQuery = (from s in _maternalvisitvaluedb.Value select s).ToList();
I think the exception is being caused by the toList() conversion because when I remove .ToList() it works fine but I need to use that for the rest of the code to work properly. Here is some part of the code:
MaternalVisitData _maternalvisitvaluedb =
new MaternalVisitData(MaternalVisitData.strConnectionString);
MaternalCareData _maternalcarevaluedb =
new MaternalCareData(MaternalCareData.strConnectionString);
var firstQuery = (from s in _maternalvisitvaluedb.Value select s).ToList();
var secondQuery = (from t in _maternalcarevaluedb.Value select t).ToList();
var result = (from s in firstQuery
join k in secondQuery
on s.MotherId equals k.MotherId
where (DateTime)s.SecondVisit.Date == DateTime.Now.Date
select s).ToList();
Thanks for your help!
try specific type alternate of var in below lines :
var firstQuery = (from s in _maternalvisitvaluedb.Value select s).ToList();
var secondQuery = (from t in _maternalcarevaluedb.Value select t).ToList();
like this :
List<maternalvisitvaluedb> firstQuery = (list<maternalvisitvaluedb>)(from s in _maternalvisitvaluedb.Value select s).ToList();
List<maternalcarevaluedb> secondQuery = (List<maternalcarevaluedb>)(from t in _maternalcarevaluedb.Value select t).ToList();
it's a pseudo , I hope it helps you
Related
I am using SqlKata to create Db query for SqlLite.
var compiler = new SqliteCompiler();
var result = compiler.Compile(query);
var query = new Query()
.Select("client.Id")
.From("schema.client as c")
.Join("schema.order as o", "c.Id", "o.ClientId")
.WhereIn("c.Id", new[] {1,2,3});
Expected sql should be:
SELECT client.Id FROM "schema.client" AS c
INNER JOIN "schema.order" AS o ON c.Id = o.ClientId
WHERE c.Id IN (1,2,3)
But I am getting:
SELECT "client"."Id" FROM "schema"."client" AS c
INNER JOIN "schema"."order" AS o ON "c"."Id" = "o"."ClientId"
WHERE "c"."Id" IN (1,2,3)
When I am using schema schema.order I should not have in query: "schema"."order"
Only table name should be with "". For example it should be: WHERE c.Id and not: WHERE "c"."Id"
I would like to know, if I am doing something wrong.
Thank you very much for answer.
I have below sql query which I want to convert into LINQ to obtain exactly same results and returned by below query
select *
from (
select distinct DocID
from UserViewDoc
where UserViewDoc.UVID in (102558)) a
left outer join
(
select distinct UserViewDoc.DocID
from UserViewDoc
inner join UserViewHeader on UserViewDoc.UVID = UserViewHeader.UVID
where UserViewDoc.UVID not in (102558)
and UserViewHeader.IsLock = 1) b on a.DocID = b.DocID
where b.DocID is null
)
What I have tried so far is below LINQ statement
var v = (from uvd in this.ViewSelectorControl.LDReviewContext.GetTable<UserViewDoc>()
where IDs.Contains(uvd.UVID)
select new { uvd.DocID, uvd.UVID });
var c = ((from uvd in this.ViewSelectorControl.LDReviewContext.GetTable<UserViewDoc>()
join uvh in this.ViewSelectorControl.LDReviewContext.GetTable<UserViewHeader>() on uvd.UVID equals uvh.UVID
where !IDs.Contains(uvh.UVID) && uvh.IsLock == true
select new { uvd.DocID, uvd.UVID } ));
var d = (from id in v
join ids in c on id.UVID equals ids.UVID into vc
from sub in vc.DefaultIfEmpty()
where sub == null
select id);
The problem I am facing is running the SQL query is returning 30583 records and LINQ version of it is returning all of the 30613 records
I rewrote the query to be exactly like the sql query, i believe that this is the way:
var firstQuery = (from u in this.ViewSelectorControl.LDReviewContext.GetTable<UserViewDoc>()
where IDs.Contains(u.UVID)
select u.DocID).Distinct();
var innerQuery = (from u in this.ViewSelectorControl.LDReviewContext.GetTable<UserViewDoc>()
join uvh in this.ViewSelectorControl.LDReviewContext.GetTable<UserViewHeader>() on uvh.UVID equals u.UVID
where IDs.Contains(u.UIVD) == false
&& uvh.IsLock
select u.DocID).Distinct();
var resultQuery = from f in firstQuery
join i in innerQuery on i equals f into lout
from i in lout.DefaultIfEmpty()
where i == null
select f;
I'm new to sql-linq and I'm trying to join two tables using their common id which is motherid. I have done that but when I try to convert the returned value into list it throws an exception saying "The query contains references to items defined on a different data context." Here is the code.
var todaySecondVisitProfile = (from a in _maternalvisitvaluedb.Value
join b in _maternalcarevaluedb.Value on a.MotherId equals b.MotherID
where (DateTime)a.SecondVisit.Date == DateTime.Now.Date
select new
{
FirstName = b.FirstName,
LastName = b.LastName,
PhoneNo = b.PhoneNo
}).ToList();
If I can't convert the result into list how can I access my result? tnx for the help.
I think you are trying to do a Linq on two different databases. If so then you should so something like this:
var firstQuery = (from s in _maternalvisitvaluedb.Value select s).ToList();
var secondQuery = (from t in _maternalcarevaluedb.Value select t).ToList();
var result = (from s in firstQuery
join k in secondQuery
on s.MotherId equals k.MotherId
where (DateTime)s.SecondVisit.Date == DateTime.Now.Date
select s).ToList();
I have a problem creating the following SQL Statement using LINQ & C#
select c.IDAddenda, c.Descripcion
from CatAddendas c
right join EmpresaAddenda e on e.IDAddenda = c.IDAddenda
where e.rfc = 'SUL010720JN8'
order by c.IDAddenda asc
I got this:
public IEnumerable<CatAddenda> TraeAddendas(string rfc)
{
DataClasses1DataContext dc = new DataClasses1DataContext(...);
return (from adds in dc.EmpresaAddendas
cats.IDAddenda into joined
where adds.RFC == rfc
select adds.CatAddenda);
}
This is not doing a right join, so any ideas?
var RightJoin = from adds in dc.EmpresaAddendas
join cats in CatAddendas
on adds.IDAddenda equals cats.IDAddenda into joined
from cats in joined.DefaultIfEmpty()
select new
{
Id = cats.IDAddenda,
Description = cats.Descripcion
};
var results = from e in EmpresaAddenda
join c in CatAddendas
on e.IDAddenda equals c.IDAddenda into f
from c in f.DefaultIfEmpty()
select new
{
ID = c.IDAddenda,
Description = c.Descripcion
};
You can apply where and order by on the results.
I have two tables:
PlanMaster (PlanName, Product_ID)
and
ProductPoints (Entity_ID, Product_ID, Comm1, Comm2)
Now I am storing Entity_ID into a Session which is stored into an 'int':
int getEntity = Int16.Parse(Session["EntitySelected"].ToString());
I want to show in my LINQ query all of the items from above tables which has
Entity_ID = getEntity
Here is my LINQ query:
var td = from s in cv.Entity_Product_Points join r in dt.PlanMasters on s.Product_ID equals r.Product_ID
where s.Entity_ID = getEntity
select s;
Now its giving me an error which says:
Cannot implicitly convert type 'int?' to 'bool'
What is going wrong here? Thank you for your comments in advance!
Try changing it to
where s.Entity_ID == getEntity
var td =
from s in cv.Entity_Product_Points
join r in dt.PlanMasters on s.Product_ID equals r.Product_ID
where s.Entity_ID == getEntity
select s;
= not equal to ==
where s.Entity_ID = getEntity should be where s.Entity_ID == getEntity.
Shouldn't that be a double equals?
var db1 = (from a in AccYearEntity.OBLHManifests select a).ToList();
var db2 = (from a in MasterEntity.UserMasters select a).ToList();
var query = (from a in db1
join b in db2 on a.EnteredBy equals b.UserId
where a.LHManifestNum == LHManifestNum
select new { LHManifestId = a.LHManifestId, LHManifestNum = a.LHManifestNum, LHManifestDate = a.LHManifestDate, StnCode = a.StnCode, Operatr = b.UserName }).FirstOrDefault();
I think this will do,
where s.Entity_ID == getEntity