I have a table with a list of keywords like
Waiter
Receptionist
Cleaner
Painter
And I have another table that has a Job Title.
I want a query that tells me if any of the words in the first table are contained in a specific Job Title in the second table
Im wondering if this is possible without looping over every row in the first table.
Use this as reference and modify as needed.
SecondTable.Where(a => FirstTable.Any(b => b.Keyword == a.Keyword))
Related
I found an interesting issue in Entity Framework. Check the code bellow. Here i am using Contains() to find all matching Id rows from Table Test1 but when i add same id multiple times it returns only 1 item not duplicating items but i want to get duplicate items too. How can i do this then?
var ids = new List<int>();
ids.Add(1);
ids.Add(1);
var foo = ctx.Test1.Include("Test2").Where(x => ids.Contains(x.Id)).ToList();
YOu can not. You really need to learn the basic of how SQL works and how query works because your question is a fundamental misunderstanding.
when i add same id multiple times it returns only 1 item not duplicating items
Because the table STILL contains only 1 item. if you add the same ID multiple times, why would you expect it to return the row multiple times?
The way it is evaluated is:
Take row
Check whether the ID matches any of the provided list.
Next row.
So, regardless how often you put the ID into the list of approved id's, it OBVIOUSLY will only return one row. You do not get duplicate items because you do not have duplicate items to start with.
Like so often when using anything Ef related, it also helps to intercept and look at the generated SQL and the generated query plan - this at least will make obviously clear that you can not get 2 id's. Contains will be an IN clause, containing the list of values. Like I said above, contains checks for rows, it will not magically duplicate them.
I would suggest making the duplication manually after query - though in 25 years I have never seen this requirement coming up, so I would strongly suggest you check whether what you try to do makes any logical sense from a higher perspective first.
Why should it be the other way? Your EF Contains instruction has in SQL "IN" form:
SELECT
...
FROM ...
WHERE ... IN (1, 1)
I am a newbie with LINQ so I would appreciate if you could explain thoroughly your answer. What I am trying to do is that I have a table called ContentTable. This ContentTable has different columns and rows and one of the columns is using EntityId's.
In my method I receive from somewhere a list of ID's in a List. I don't want to iterate over this list and query the database for each ID because that would be expensive, but I want a query which uses the ID's in the list to match with the EntityID's in the table, and if there is such a match, I erase the whole row from the table. May you kindly help me? Thanks in advance!
You can use Entity Framework Extensions to do that. Something like:
this.Container.Devices.Delete(
o => o.Id == 1,
o => o.Id == 2
);
Some thing like this might help , however if the table is too big (millions) Linq to sql will lock the table. Also for each entity it issues a single T-SQL Delete statement to delete the entity.
public void DeleteContents(List<int> entityIds)
{
using (Yourcontext db = new Yourcontext())
{
IQueryable<ContentTable> contents = db.ContentTable.Where((x) => ids.Contains(x.EntityId));
db.ProductComplianceRules.DeleteAllOnSubmit(contents);
}
}
Sounds trivial but I cannot find an elegant answer to this: how do I read all rows of a specific column into a list of strings for instance using LINQ on Entity Framework context?
You could try something as simple as the following:
var rows = dbContext.TableName.Select(x=>x.ColumName);
where dbContext is the class you use to "talk" with your database, TableName is the name of the table, whose column values you want to read and ColumnName is the name of the column.
Furthermore, if you place a ToList after the Select, you will create list of objects whose type would be the type of the values in column called ColumnName.
Christos answer will just give you an IQueryable. If you want an actual List you need to do something with the IQueryable:
var rows = dbContext.TableName.Select(x=>x.ColumName).ToList();
though I might go for the LINQ syntax:
var rows = (from c in dbContext.TableName
select c.ColumnName).ToList();
The two forms are equivalent.
im calling a table with 200.000 rows and 6 columns, but i only want 2 of these columns to be used in one controller, so i want to know if there is a better way to call them from the server without compromising performance, because as i know Linq queries get the whole table and them makes the filtering, i think maybe Views is a good way, but i want to know if there are others and betters, Thanks.
for example:
var items = from i in db.Items select new {i.id,i.name};
in case i have 1.000.000 items, will it be a trouble for the server?
Your initial assumption is incorrect.
In general LINQ queries do not get the whole table. the query is converted into a "server side expression" (i.e. a SQL statement) and the statement is resolved on the server and only the requested data is returned.
Given the statement you provided you will return only two columns but you will get 1,000,000 objects in the result if you do not do any filtering. But that isn't a problem with LINQ, that's a problem with you not filtering. If you included a where clause you would only get the rows you requested.
var items = from i in db.Items
where i.Whatever == SomeValue
select new { i.id, i.name };
Your original query would be translated (roughly) into the following SQL:
SELECT id, name FROM Items
You didn't include a where clause so you're going to get everything.
With the version that included a where clause you'd get the following SQL generated:
SELECT id, name FROM Items WHERE Whatever = SomeValue
Only the rows that match the condition would be returned to your application and converted into objects.
I have 2 datatables.
One is the master the other is the detail
When someone goes to delete a master record how can I check that there are no rows in my detail datatable that relate to the master table's ID.
UPDATE: The user has the ability to select more than one master record at a time
I want to use LINQ if possible.
I started looking at DataTable.Rows.Cast()......
You don't need LINQ for this.
Instead, you can check masterRow.GetChildRows("RelationName").Length.
EDIT: You should use a DataRelation.
If you really don't want to, you can check
childTable.AsEnumerable().Any(dr => dr["ParentIdColumn"] == someValue)
EDIT: To check for multiple parents:
var parentKeys = parentRows.Select(dr => dr["id"]).ToList();
if (childTable.AsEnumerable().Any(dr => parentKeys.Contains(["ParentIdColumn"])))