I want to have join query from a table with a dictionary based on a common field, my query is:
var query = from c in db.Exp
join d in lstUniprotDic on c.UniID equals d.Key
select new
{
c.UniID,
IdentityPercent=d.Value.ToString(),
c.PrId,
c.SpotNo
}
but i got the following error
Local sequence cannot be used in LINQ to SQL implementation of query operators except the Contains() operator.
That pretty much says it all. You can't use the dictionary in your LINQ to SQL query except when using Contains.
Solution:
(from c in db.Exp where lstUniprotDic.Keys.Contains(c.UniID) select c).AsEnumerable()
join d in lstUniprotDic on c.UniID equals d.Key
select new
{
c.UniID,
IdentityPercent=d.Value.ToString(),
c.PrId,
c.SpotNo
}
I am not sure if the usage of lstUniprotDic.Keys in the LINQ to SQL query is actually working.
If not, try using this code instead:
var ids = lstUniprotDic.Keys.ToArray();
(from c in db.Exp where ids.Contains(c.UniID) select c).AsEnumerable()
join d in lstUniprotDic on c.UniID equals d.Key
select new
{
c.UniID,
IdentityPercent=d.Value.ToString(),
c.PrId,
c.SpotNo
}
Related
I have the following query
SELECT sdb.student_due_by_month_id, sdb.due_month_year, sum(sdb.due_total_month) FROM user_student ust
INNER JOIN student std ON ust.student_id = std.student_id
INNER JOIN student_due_by_month sdb ON std.student_id = sdb.student_id
WHERE ust.user_id = 2
GROUP BY sdb.student_due_by_month_id, sdb.student_due_by_month_id, sdb.due_month_year
I need to convert it to linq or a lambda expression.
I'm trying to turn him into a linq and this is what I have so far:
var user_due_month = await (from ust in _context.Users_students
join std in _context.Students on ust.student_id equals std.student_id
join sdb in _context.Students_dues_by_months on std.student_id equals sdb.student_id
where ust.user_id == user_id
group sdb by new { sdb.student_due_by_month_id, sdb.due_month_year } into g //from here i have yet to complete the linq query, help?
I don't know what would be missing to complete my linq query correctly, and also be able to pass it to a viewmodel.
Thanks a lot!!
I am using Linq to Sql to get two lists than take Union with another list. They are working fine. But I am wondering if it can be done in one Query or two instead of three that I have.
Querys are
var l = (from t in T_list1
where t.Date == DateTime.Today
select new
{
oldDate=t.OldDate,
Name=t.name,
Email=t.EmailAddress,
list2TableId=t.l2Id,
CustomerId=t.customerId
});
var l2=(from d in T_list2
from e in l1
where d.Id == e.list2TableId
select new
{
Date=d.oldDate,
CName=d.Name,
Experience=e.experience,
});
list2.Dump();
var l3 = list2.Union(list3).ToList();
I was looking at this post but didnt work. Combining 2 Linq queries into 1
Thanks for your input.
You could do a join instead of the 2 queries:
var l = (from t in T_list1
join d in T_list2 on t.l2Id equals d.Id
where t.Date == DateTime.Today
select new
{
oldDate=t.OldDate,
Name=t.name,
Email=t.EmailAddress,
list2TableId=t.l2Id,
CustomerId=t.customerId
Date=d.oldDate,
CName=d.Name
Experience=e.experience,
});
I haven't tested the query but it should show you the rough idea.
Have a look at JOINs in SQL & Linq to get more information.
I want to convert this query into linq, please help:
select
mr_ssample.objectid,
mr_ssample.stcode
from mr_ssample
inner join mr_wsample on mr_ssample.objectid = mr_wsample.objectid
And mr_ssample.stcode in( select stcode
from mr_wsample)
i tried this in C#
var query = from ssamp in marineEntity.MR_SSAMPLE
join wsamp in marineEntity.MR_WSAMPLE on ssamp.OBJECTID equals wsamp.OBJECTID && ssamp.stcode.contains(wsamp.stcode)
select new
{};
However, I could not access wsamp in contains, or I dont know the alternative of this.
give this a try,
var _result = from a in mr_ssample
join b in mr_wsample on a.objectid equals b.objectid
where (from c in mr_wsample select new {c.stcode})
.Contains(new {a.stcode})
select new {a.objectid, a.stcode}
How can I write this HQL in Linq:
select a from A a
join a.childrenList b
where b = 1
childrenList is a list of enums which is not mapped to database by type but
rather is saved with its integer value.
This HQL works fine but I want to write it in Linq.
I cannot write something that can be compiled.
I think you can do
var results =
from a in db.Query<A>()
where a.childrenList.Any(b => b == (B)1)
select a;
or, using chained methods:
var results = db.Query<A>().Where(a => a.childrenList.Any(b => b == (B)1));
Regarding our comments above, I think you can drop the from A a in ... select a statements, because they are redundant.
I had tried to join two table conditionally but it is giving me syntax error. I tried to find solution in the net but i cannot find how to do conditional join with condition. The only other alternative is to get the value first from one table and make a query again.
I just want to confirm if there is any other way to do conditional join with linq.
Here is my code, I am trying to find all position that is equal or lower than me. Basically I want to get my peers and subordinates.
from e in entity.M_Employee
join p in entity.M_Position on e.PostionId >= p.PositionId
select p;
You can't do that with a LINQ joins - LINQ only supports equijoins. However, you can do this:
var query = from e in entity.M_Employee
from p in entity.M_Position
where e.PostionId >= p.PositionId
select p;
Or a slightly alternative but equivalent approach:
var query = entity.M_Employee
.SelectMany(e => entity.M_Position
.Where(p => e.PostionId >= p.PositionId));
Following:
from e in entity.M_Employee
from p in entity.M_Position.Where(p => e.PostionId >= p.PositionId)
select p;
will produce exactly the same SQL you are after (INNER JOIN Position P ON E..PostionId >= P.PositionId).
var currentDetails = from c in customers
group c by new { c.Name, c.Authed } into g
where g.Key.Authed == "True"
select g.OrderByDescending(t => t.EffectiveDate).First();
var currentAndUnauthorised = (from c in customers
join cd in currentDetails
on c.Name equals cd.Name
where c.EffectiveDate >= cd.EffectiveDate
select c).OrderBy(o => o.CoverId).ThenBy(o => o.EffectiveDate);
If you have a table of historic detail changes including authorisation status and effective date. The first query finds each customers current details and the second query adds all subsequent unauthorised detail changes in the table.
Hope this is helpful as it took me some time and help to get too.