convert a lambda expression from C# to vb.net - linq

hello evry one and thank you for your help, I really need to convert this C# lambda expression into VB.NET ?
var res = (from v in initialList
group v by v.entityId into grp
select grp.OrderByDescending(v => v.gpsDate).First()).ToList();

This should work. LINQPad is your friend.
Dim res = (From v In initialList
Group v By v.entityId Into grp = Group
Select grp.OrderByDescending(Function(v) v.gpsDate).First()).ToList

Related

Select from two tables using "NOT IN" in LINQ in Entity Framework

i have the following SQL, how can i implement that in LINQ, i am using Entity framework in my MVC project.
SELECT
* FROM Catalog.ServiceItem
WHERE SKU NOT IN (SELECT ServiceItemSKU FROM Catalog.TaggedServiceItems)
Any Ideas ?
EDIT:
ANSWER:
var untaggedItems = from item in serviceItems where !taggedServiceItems.Contains(item.SKU) select item;
You can do something like this with Contains:
var result = from item in ServiceItem
where !TaggedServiceItems.Contains(item.SKU)
select item;
Try this
var Catalog_ServiceItem = ds.Tables["Catalog.ServiceItem"].AsEnumerable();
var Catalog_TaggedServiceItems = ds.Tables["Catalog.TaggedServiceItems"].AsEnumerable();
var _except = from c in Catalog_ServiceItem
join b in Catalog_TaggedServiceItems
on c.Field<string>("ServiceItemSKU ") equals b.Field<string>("ServiceItemSKU ") into j
from x in j.DefaultIfEmpty()
where x == null
select c;
EDIT
You can do this
var _except = Catalog_ServiceItem.Except(Catalog_TaggedServiceItems);
use lambda.
dim query=Catalog.ServiceItem.where(function(q) not q.Contains(q.SKU))
Try this:
var untaggedItems =
from i in Catalog.ServiceItem
let taggedItems = (
from ti = Catalog.TaggedServiceItems
select ti.ServiceItemSKU ).Distinct()
where ! taggedItems.Contains( i.SKU )
select i;

how to compare unique identifier with Linq

I have a view and entity
var q = from stud in context.CollegePlans
where stud .Meta.Active == true &&
stud .CreatedBy_Id == user.Id
select stud ;
var k = from nv in context.vw_Year_Plans
where ( nv.StudentId == q.Where( p => p.Section.StudentId))
select nv;
studenId is Guid in both view and in entity..when i do the above it says cannot implictly convert system.Guid ? to bool ..how to overcome this ?
The expression evaluated by Where needs to evaluate to a boolean value.
where ( nv.StudentId = q.Where( p => p.Section.StudentId))
In this case, you have given it a Guid. That is where the error is coming from. I think you would have problems with that line even if the Where predicate was valid. From the looks of it, you want to want all of the StudentIds in context.vw_Year_Plans that are also in q.
A simple way to do that is to create an intermediate collection of the ids in q for comparison purposes.
var temp = q.Select(a => a.Section.StudentId);
Then see if each StudentId is also in context.vw_Year_Plans
var k = from nv in context.vw_Year_Plans
where (temp.Contains(nv.StudentId))
select nv;
There are better ways to solve this issue, but this should get you going.
EDIT
You mentioned using join in your comment. I haven't tested either of these solutions but this should be what you need. If you are still having problems, update your question with more specific details on the issues you are facing.
var k = from nv in context.vw_Year_Plans
join p in q on nv.StudentId equals p.Section.StudentId
select nv;

Simple Linq grouping

how to use group by in Linq.
SqlServer Sample
select
ActionGroupName,
sum(cast(isnull(solicit_count,0) as int)) as 'SolicitCount'
from
[solicits]
group by
ActionGroupName
my linq version
var groupbyfilter = from v in lstSale
group v by v.ActionGroupName into g
select g.ActionGroupName, sum(g.solicit_count)
i am not getting it in proper way.
Try:
var groupbyfilter = from v in lstSale
group v by v.ActionGroupName into g
select new
{
ActionGroupName = g.Key,
Sum = g.Sum(x => x.solicit_count)
};

LINQ - using a query expression to calculate data time difference

I use EntityFramework 4, LINQ, and C#.
I need display data in a GridView from a query expression using Linq; I need project as anonymous type a value calculate on fly DateTime.UtcNow - cl.DateLocked Note: cl.DateLocked is of type DateTime and I need to know how many days of difference between this two dates.
With this query I receive an error:
DbArithmeticExpression arguments must have a numeric common type.
Any idea how to do it in Linq? If Linq does not allow it how to do it in a different way?
Thanks for your time
var queryContentsLocked = from cl in context.CmsContentsLockedBies
join cnt in context.CmsContents
on cl.ContentId equals cnt.ContentId
join u in context.aspnet_Users
on cl.UserId equals u.UserId
select new
{
cl.DateLocked,
cnt.ContentId,
cnt.Title,
u.UserName,
u.UserId,
TimePan = DateTime.UtcNow - cl.DateLocked // Problem here!!!
};
var queryContentsLocked = from cl in context.CmsContentsLockedBies
join cnt in context.CmsContents
on cl.ContentId equals cnt.ContentId
join u in context.aspnet_Users
on cl.UserId equals u.UserId
select new
{
cl.DateLocked,
cnt.ContentId,
cnt.Title,
u.UserName,
u.UserId,
Days = SqlFunctions.DateDiff("day", cl.DateLocked, DateTime.UtcNow)
};
Without using any SqlFunction
var grandTotalWork = (from t in db.AssignmentHandymandetails
join ad in db.AssignmentDetails on t.assignment_detail_id equals ad.assignment_detail_id
where ad.assignment_id == Convert.ToInt32(Label_assignmentId.Text)
select new { startTime = t.started_time.Value.TimeOfDay, endTime = t.end_time.Value.TimeOfDay, TotalWorkTime = (t.started_time.Value.TimeOfDay.Duration() - t.end_time.Value.TimeOfDay.Duration()).Duration()});
then you can bind the result in GridView you wish
Try this:
DateTime.UtcNow.Subtract(cl.DateLocked).TotalDays

converting group SQL expression into LINQ

I am having a real pain in converting this query expression into my LINQ expression.
SELECT
r.Disc_code
,r.SEX
FROM RACE r
WHERE r.EVENT_CODE = 100
GROUP BY
r.SEX
, r.disc_Code
order by r.disc_code
i can work with one table but i have not seen any example which chains two group expressions in stackoverflow or MSDN. Am i missing some thing ?
Maybe something like this:
var results = from r in dataContext.Race
where r.EVENT_CODE == 100
orderby r.Disc_Code ascending
group r by new { r.Sex, r.Disc_Code } into g
select g.Key;
Here's an example of grouping by multiple columns in VB.NET.
Dim query = From r In db.Race _
Where r.EVENT_CODE = 100 _
Order By r.disc_Code _
Group By Key = New With {r.Sex, r.disc_Code} Into Group _
Select Key, Group
to group on multiple criteria you have to do something like this:
var query = from book in books
group book by new {book.author, book.editor} into books;
to access them:
var author = books.Key.author;
var editor = books.Key.editor;

Resources