LINQ query NOT IN - linq

I have this simple SQL query using NOT IN operator:
SELECT * FROM List l WHERE L.ListName NOT IN ('POType','MailClass')
How can I write this using LINQ query syntax?

string[] listNames = {"POType", "MailClass"};
var query = from l in List
where ! listNames.Contains(l.ListName)
select l;

Related

Comparable SQL for LINQ query?

Is there an SQL query that would give me the same result as the following LINQ statement?
DataTable dtAll1 = userData1.AsEnumerable()
.Where(ra => !userData2.AsEnumerable()
.Any(rb => rb.Field<string>("A") == ra.Field<string>("B")))
.CopyToDataTable();
SELECT
*
FROM
userData1 AS ra
WHERE
NOT EXISTS (SELECT * FROM userData2 AS rb WHERE ra.B = rb.A)
(Updated from EXISTS to NOT EXISTS.)

Rewrite SQL to LINQ query

Hi I am trying to write a SQL query to LINQ query. My SQL query is like this:
select BookingArrivedEnquiredTime
from BookingArriveds
where BookingArrivedEnquiredDateTime='2015-02-17 00:00:00.000'
order by CAST(('01/01/2000 ' + BookingArrivedEnquiredTime) AS DATETIME)
How can I write this query in LINQ?
You might try just parsing the string as DateTime:
var res = from item in BookingArrivedEnquiredTime.BookingArriveds
where item.BookingArrivedEnquiredDateTime == DateTime.Parse("2015-02-17 00:00:00.000")
orderby DateTime.Parse("01/01/2000 " + item.ToString())
select item;

LINQ Syntax for query using Sum()

I want to calculate a Sum value using fields from 2 tables and can't work out the syntax.
var x = (from l in context.Lessons
join t in context.Tariffs on l.TariffId equals t.Id
where l.StudentSemesterId == studentSemesterId
select new {
lessonCost = (t.Rate) * (l.Duration / 60)
});
This returns a set of values for individual lessons. How do I get the Sum total of these lessons?
You are selecting a new IEnumerable of an anonymous object type. Just select the correct value you are looking for. Then you have IEnumerable of float/double/decimal/whatever. Then take the resulting sum of that query.
var x = (from l in context.Lessons
join t in context.Tariffs on l.TariffId equals t.Id
where l.StudentSemesterId == studentSemesterId
select (t.Rate) * (l.Duration /60)).Sum();

Linq: Orderby when including multiple tables

Currently learning Linq to Entity. I been successful, but came stumped with the orderby clause and its use with multiple tables.
var query = from k in contxt.pages.Include("keywords")
where k.ID == vals.pageId select k;
My understanding with the code above is it creates an inner join where ID is equal to pageId.
So what I am having a difficult time visualizing is how I would perform an orderby on both tables?
I would like to sort on both tables.
I have tried:
var query = from k in contxt.pages.Include("keywords") where k.ID == vals.pageId orderby k.keywords.**?** select k;
The question mark is not supposed to be there. I am showing that the column that I would like to sort by isn't there. Trying this k.Kegwords. doesn't show the column.
I would write a SQL query as follows:
string query = "SELECT pages.page, pages.title, pages.descp, keywords.keyword
FROM pages INNER JOIN keywords ON pages.ID = keywords.pageID
ORDER BY keywords.sort, pages.page";
pages and keywords have a 1 to many relationship, which FK keywords.
Thank you,
deDogs
Here you go.
var result = (from x in pages
join y in keywords on x.ID equals y.pageID
orderby y.sort, x.page
select new
{
x.Page,
x.title,
x.descp,
y.keyword
});

LINQ execute SQL query with output parameter

I need to execute SQL query with output parameter.
For example,
SELECT #Count = COUNT(*) FROM dbo.SomeTable
SELECT * FROM SomeTable WHERE Id BETWEEN 1 AND 10
After quering I need to know the #Count value.
How can I do it with LINQ without using a stored procedure?
Thank you.
int value = yourDB.SomeTable.Count(q=>q.id >=1 && q.id <= 10);
linq is pretty easy :)
edit: so you want 2 items, the count, and then a limited part of the array.
List<SomeTable> li = yourDB.SomeTable.ToList();
int number = li.Count;
List<SomeTable> partial = li.GetRange(0, 10);
or
int value = yourDB.SomeTable.Count();
List<SomeTable> partial = yourDB.SomeTable.ToList().GetRange(0, 10);
so the best LINQ thing for paging is:
List<SomeTable> partial = yourDB.SomeTable.OrderBy(q=>q.id).Skip(0).Take(10).ToList();

Resources