I trying to write sql query to linq:
Query:
select s.s_name, sub.state, sub.to, sub.evaluation, sub.task_id
from submit_task sub
join student s on s.id=sub.student_id
join task t on t.id=sub.task_id
where t.t_name = "bbbb";
Linq:
var subTask = (from sub in ado.submit_task
join s in ado.student on sub.student_id equals s.id
join t in ado.task on sub.task_id equals t.id
where t.t_name == listView3.SelectedItems[0].Text
select new { s.s_name, sub.state, sub.to,
sub.evaluation, sub.task_id });
but this not working. When I try dubugg, nothing's happened, with no error or result. Do you see some mistake ??
thankk you
var text = listView3.SelectedItems[0].Text;
var subTask = (from sub in ado.submit_task
join s in ado.student on sub.student_id equals s.id
join t in ado.task on sub.task_id equals t.id
where t.t_name == text
select new { s.s_name, sub.state, sub.to, sub.evaluation, sub.task_id });
Related
I want to check values that are present in other table, not from fixed list or array. here is an example of what I need. Actually I am querying EF.
Database_EF db = new Database_EF();
var listA = (from a in db.a
where a.id in
(from b in db.b
join c in db.c on b.id equals c.id
where c.col1 equals 'something'
select b.id)
select a.id).ToList();
I am new to linq. Thanks.
Should be something like this:
var listA = (from a in db.a
where (from b in db.b
join c in db.c on b.id equals c.id
where c.col1 == "something"
select b.id).Contains(a.id)
select a.id).ToList();
I have two tables
1) T_EJV_CREDIT_DS_INDEX
2) T_EJV_CREDIT_DS_INDEX_CONTRACT
I would like a SQL query like below as a LINQ expression
SELECT MAX(INDEX_FAMILY_VERSION) FROM T_EJV_CREDIT_DS_INDEX cdi
INNER JOIN T_EJV_CREDIT_DS_INDEX_CONTRACT cdic
ON cdic.INDEX_ID = cdi.INDEX_ID
WHERE cdi.INDEX_SHORT_NAME LIKE '%#VARIABLE1%'
AND cdic.TENOR = #VARIABLE2
This is what I have attempted so far
var maxFamilyVersion = (from ic in dsIndexContract
join i in dsIndex on i.INDEX_ID equals ic.INDEX_ID
where i.INDEX_SHORT_NAME.CONTAINS(strindex) && ic.TENOR equals d.TERM
select new
{
ic.INDEX_FAMILY_VERSION.Max()
}).Take(1).ToList();
But the above mentioned starts showing compile issues with the syantax as shown below
Checking for equality in your where condition can be done with ==. The keyword equals is only used in a join condition.
var result = (from ic in dsIndexContract
join i in dsIndex on i.INDEX_ID equals ic.INDEX_ID
where i.INDEX_SHORT_NAME.CONTAINS(strindex) && ic.TENOR == d.TERM
select new
{
ic.INDEX_FAMILY_VERSION.Max()
}).FirstOrDefault();
And instead of .Take(1).ToList(), you can use .FirstOrDefault() to retrieve the first item.
Or a more efficient way is to use .Max() directly instead of .FirstOrDefault():
var result = (from ic in dsIndexContract
join i in dsIndex on i.INDEX_ID equals ic.INDEX_ID
where i.INDEX_SHORT_NAME.CONTAINS(strindex) && ic.TENOR == d.TERM
select ic.INDEX_FAMILY_VERSION).Max();
This should do it:
var maxFamilyVersion =
(from ic in dsIndexContract
join i in dsIndex on ic.INDEX_ID equals i.INDEX_ID
where i.INDEX_SHORT_NAME.CONTAINS(strindex) && ic.TENOR == d.TERM
select ic.INDEX_FAMILY_VERSION).Max();
In building a patient appointment application I need to return patients with its navigation property patientDetails while those returned match a sessionId that is not directly deduced from the patient but rather from a series of other navigation properties. This in itself is not difficult, like plain SQL joins can be used, it's just that my navigation property patientDetails is never included.
The include path is correct, in case anyone asks.
using (DbEntities db = new DbEntities())
{
List<tblPatient> res = (from s in db.tblSessions
join b in db.tblBookings on s.id equals b.sessionId
join r in db.tblReferrals on b.referralId equals r.id
join a in db.tblAttendanceStatus on b.attendanceStatus equals a.id
join p in db.tblPatients.Include("tblPatientDetail") on r.patientId equals p.id
join pd in db.tblPatientDetails on p.patientDetailsId equals pd.id
where s.id == id
select p).ToList();
return res;
}
It appears to make no difference whether or not I include the .include.
What have I overlooked?
Includes are ignored when you use join or group by. You can change your query to use where instead
List<tblPatient> res = (from p in db.tblPatients.Include("tblPatientDetail")
where
(from s in db.tblSessions
join b in db.tblBookings on s.id equals b.sessionId
join r in db.tblReferrals on b.referralId equals r.id
join a in db.tblAttendanceStatus on b.attendanceStatus equals a.id
where r.patientId == p.id
where s.id == id
select 1).Any()
select p).ToList()
I have this query below. After that, I want to populate a list with the results. How should I do ?
var query = from cust in context.Customer
join city in context.Cities on cust.id_city equals city.id
join state in context.State on city.id_state equals state.id
join reg in context.Region on state.id_region equals reg.id
select new
{
nameCust = cust.name,
nameCity = city.name,
nameState = state.name,
nameRegion = reg.name
};
You should be able to call ToList() on the return value:
var queryResult = (from cust in context.Customer
join city in context.Cities on cust.id_city equals city.id
join state in context.State on city.id_state equals state.id
join reg in context.Region on state.id_region equals reg.id
select new
{
nameCust = cust.name,
nameCity = city.name,
nameState = state.name,
nameRegion = reg.name
}).ToList();
I'm wondering though, if you are using Entity Framework. If so: this might be a bether solution:
var queryResult = context.Customer
.Include(customer => customer.City)
.Include(customer => customer.State)
.Include(customer => customer.Region)
.ToList();
( ! ) Keep in mind you are loading a complete table into memory using ToList()
I have a beginners LINQ2SQL question. I have this huge (but not complex) SQL statement:
SELECT Artikel.ArtikelID,
Artikel.CategorieID,
Artikel.ImageFile,
Artikel.RetailPrijs,
ISNULL(ShopArtikel.VerkoopsPrijs, Artikel.VerkoopsPrijs) AS VerkoopsPrijs,
Artikel.ArtikelCode,
Artikel.InAssortimentSinds,
ArtikelTaal.ArtikelNaam,
ArtikelTaal.ArtikelOmschrijving
FROM Artikel
INNER JOIN ArtikelTaal ON Artikel.ArtikelID = ArtikelTaal.ArtikelID
INNER JOIN ShopArtikel ON Artikel.ArtikelID = ShopArtikel.ArtikelID
INNER JOIN Categorie ON Artikel.CategorieID = Categorie.CategorieID
INNER JOIN CategorieTaal ON Categorie.CategorieID = CategorieTaal.CategorieID
INNER JOIN Shop ON ShopArtikel.ShopId = Shop.ShopID
INNER JOIN CategorieGroepShop ON Shop.ShopID = CategorieGroepShop.ShopId
INNER JOIN Taal ON ArtikelTaal.TaalCode = Taal.TaalCode AND CategorieTaal.TaalCode = Taal.TaalCode
INNER JOIN CategorieGroepTaal ON Taal.TaalCode = CategorieGroepTaal.TaalCode AND CategorieGroepShop.CategorieGroepId = CategorieGroepTaal.CategorieGroepID
INNER JOIN CategorieGroep ON Categorie.CategorieGroepID = CategorieGroep.CategorieGroepID AND CategorieGroepTaal.CategorieGroepID = CategorieGroep.CategorieGroepID AND CategorieGroepShop.CategorieGroepId = CategorieGroep.CategorieGroepID
WHERE (Shop.ShopID = 23) AND
(Taal.TaalCode = 'dut') AND
(Artikel.Onzichtbaar = 0) AND
(Artikel.NietBestelbaar = 0) AND
(Categorie.Onzichtbaar = 0) AND
(Artikel.Voorraad >= Artikel.LevertijdDrempel)
and I am converting this to LINQ and have this:
var allProducts = from artikelen in dc.Artikels
join sa in dc.ShopArtikels on artikelen.ArtikelID equals sa.ArtikelID
join at in dc.ArtikelTaals on artikelen.ArtikelID equals at.ArtikelID
join cat in dc.Categories on artikelen.CategorieID equals cat.CategorieID
join catt in dc.CategorieTaals on cat.CategorieID equals catt.CategorieID
join catg in dc.CategorieGroeps on cat.CategorieGroepID equals catg.CategorieGroepID
join catgt in dc.CategorieGroepTaals on catg.CategorieGroepID equals catgt.CategorieGroepID
join sh in dc.Shops on sa.ShopId equals sh.ShopID
join catgs in dc.CategorieGroepShops on sh.ShopID equals catgs.ShopId
join tl in dc.Taals on new { tc1 = at.TaalCode, tc2 = catgt.TaalCode } equals new { tc1 = tl.TaalCode, tc2 = tl.TaalCode }
where sh.ShopID == shop.BLL.Business.ShopController.CurrentShop.Id
select dc.Artikels;
but I have the idea that I made some (minor) mistakes while joining.
any ideas please!
EDIT
I have rewritten the LINQ query to this:
var allProducts = from artikelen in dc.Artikels
join at in dc.ArtikelTaals on artikelen.ArtikelID equals at.ArtikelID
join sa in dc.ShopArtikels on artikelen.ArtikelID equals sa.ArtikelID
join cat in dc.Categories on artikelen.CategorieID equals cat.CategorieID
join catt in dc.CategorieTaals on cat.CategorieID equals catt.CategorieID
join sh in dc.Shops on sa.ShopId equals sh.ShopID
join catgs in dc.CategorieGroepShops on sh.ShopID equals catgs.ShopId
join tl in dc.Taals on new { tc1 = at.TaalCode, tc2 = catt.TaalCode } equals new { tc1 = tl.TaalCode, tc2 = tl.TaalCode }
join catgt in dc.CategorieGroepTaals on new { tl.TaalCode, catgs.CategorieGroepId } equals new { catgt.TaalCode, catgt.CategorieGroepID }
join catg in dc.CategorieGroeps on new { cat.CategorieGroepID, catgt.CategorieGroepID, catgs.CategorieGroepId } equals new { catg.CategorieGroepID, catg.CategorieGroepID, catg.CategorieGroepID }
where sh.ShopID == 230
select dc.Artikels;
but I have a syntax error after "dut" }
Edit 2:
changed the join and replaced "dut" with the corresponding field in the database.
still have the error after the first }
it says: type inference failed in the call to 'Join'
Some of the SQL joins have multiple join conditions, which you didn't put in the LINQ query.
If this is something that will be frequently run then you should rewrite it as a stored procedure. I believe it is too convoluted and complex for a LINQ statement - too hard to see what's going on.
There is a tool for it, but I didn't try it. May be it's usefull for you.
http://www.sqltolinq.com/
It looks like the error line is actually a "Where" cause but not "Joining".
You can actually split the whole long Linq statement into smaller Query.
so for this case, its better to split it like this:
var at = from a in dc.ArtikelTaals
where a.TaalCode == "dut"
select a;
var catt = from c in dc.CategorieTaals
where c.TaalCode == "dut"
select c;
.....
and you can join the IQueryable "at" and "catt" in your complex query later.