score:1
I want to find the EntityPermission where the HasAccess column is equal to the HasAccess property of the object where they have the same Id.
So you have a table EntityPermissions
. Every EntityPermission
in this table has at least a Boolean property HasAccess
, and a primary key in long property Id
Furthermore you have a list of objects, where every object has at least an CompareId and a HasAccess.
If I read your requirement correctly, you want all EntityPermissions with Id that is also a CompareId in your list, and that have equal HasAccess value.
So if your list has values:
{10, false}, {11, true}, {12, false},
And you have EntityPermissiont:
Id HasAccess
09 true don't want this one, Id is not in the list
10 true don't want this one, Id is in the list, but HasAccess incorrect
11 true I want this one: Id is in the list, HasAccess is correct
12 false I want this one: Id is in the list, HasAccess is correct
Normally you would use Where(x => y.Contains(x))
for this. The problem is that with this you can only select on one property.
var checkValues = new
{
new {CompareId = 10, HasAccess = false},
new {CompareId = 11, HasAccess = true},
new {CompareId = 12, HasAccess = false},
}
var result = dbContext.EntityPermissions.Select(entityPermission => new
{
ValueToCompare = new
{
CompareId = entityPermission.Id,
HasAccess = entityPermission.HasAccess,
},
Original = entityPermission,
})
// keep only those selected items that have a ValueToCompare in CheckValues
.Where(selectedItem => checkValues.Contains(selectedItem.ValueToCompare)
// from the remaining items, extract the original EntityPermission
.Select(selectedItem => selectedItem.Original);
score:0
What you're looking for is the SQL WHERE...IN () syntax.
If you're using a tool that produces SQL, what you want to do is something like this:
1) get the list of values you want to compare
2) Create a string like the following from them:
"('value1','value2','value3')"
3) then produce a query that looks like this:
SELECT * FROM EntityPermission
WHERE EntityPermission.HasAccess
IN ('value1','value2','value3')
for an ORM like Entity Framework, NHibernate, etc, you can do the following:
var results = db.EntityPermissions
.Where(x => listOfObjects
.Where(obj => obj.CompareId == EntityPermission.CompareId)
.Select(y => y.HasAccess)
.Contains(x.HasAccess))
score:0
You could do this with a table-valued-parameter and user-defined-type, then inner join:
SELECT * FROM EntityPermission ep
INNER JOIN @foo f ON f.Id = ep.Id AND f.HasAccess = ep.HasAccess
However: UDTs and TVPs are really very awkward to work with; frankly, I'd be tempted to just create two concatenated strings:
string with = string.Join(",", list.Where(x => x.HasAccess).Select(x => x.Id));
string without = string.Join(",", list.Where(x => !x.HasAccess).Select(x => x.Id));
and pass that down as parameters to use with string_split
:
SELECT *
FROM EntityPermission
WHERE (Id in (select value from string_split(@with, ',')) and HasAccess = 1)
OR (Id in (select value from string_split(@without, ',')) and HasAccess = 0)
score:0
You can first get your sql query in a result table then use LINQ to get your intended values. I know it is not most effective way but it could work.
public virtual List<YOUR_DTO> ExampleOperation(YOUR_DTO dto)
{
sqlText="SELECT * FROM EntityPermission ";
dbComm = db.GetSqlStringCommand(sqlText);
DataTable table = this.Database.ExecuteDataSet(dbComm).Tables[0];
List<YOUR_DTO> result = new List<YOUR_DTO>();
foreach (DataRow row in table.Rows)
{
result.Add(new YOUR_DTO()
{
...
});
}
//LINQ
result = result.Where(obj => obj.CompareId == EntityPermission.CompareId).HasAccess;
return result;
}
Source: stackoverflow.com
Related Articles
- LINQ Query comparing Id with an Id in a list inside SingleOrDefault query
- Comparing each element with each other element in a list
- Comparing 2 objects and retrieve a list of fields with different values
- join list with linq-to-sql query
- Linq to entities - SQL Query - Where list contains object with 2 properties (or more)
- How to write a LINQ to Entities query with List in a "WHERE" condition
- How to query nested list in Mongo C# 2.2 driver with Linq?
- EF Code First comparing null values generates strange query
- Linq Query to Compare with List of string values
- query list with linq lambda expressions
- Entity Framework query to custom object Class with List
- Comparing sql with c# list in query?
- Query expressions over source type 'dynamic' or with a join sequence of type 'dynamic' are not allowed
- LINQ query returns old results when source list is re-initialized
- How to query by where clause with EF code first
- C# - Linq optimize code with List and Where clause
- How can I speed up this linq query on a List<> with search criteria on 3 attributes in the list object
- Query filesystem for list of files with certain attributes?
- How to compile a LINQ query with a List parameter?
- c# Linq query with Tuple List
- Linq sub query when using a repository pattern with EF code first
- c# find item in list returned by LINQ query and compare its value with another item in list
- LINQ query to filter list by object property with multiple checks
- Query with LINQ where Context matches multiple parameters from a List of Objects
- LINQ query with Left Join, Group by without null List
- Linq query with select needed to get specific properties from a list
- How to order list in custom Item class with linq query
- Complex Linq query to select list items with another list property containing a value
- Why is my Linq Oracle DB query not comparing timestamp with datetime correctly?
- I can't get pagination to show up in every page on a GridView with a list as source
- Linq-to-SQL Retrieving child tables throws InvalidCastException
- No Overload for 'MouseDoubleClick method' matches delegate 'System.Windows.Input.MouseButtonEventHandler'
- Order a couple of synchronized lists based on a selected list
- LINQ Remove all Users where UserId is in the list
- Linq To Entities insert many to many but only unique values
- how to create a list of lists from one list using LINQ?
- Photon Unity Leaderboard sorting
- Group array of string arrays with LINQ
- ODAC / Oracle Equivalent to Linq to Entities GroupBy FirstOrDefault
- Refactoring LINQ to Entities queries with let variables and sub-queries
- SQL Server Vs MongoDB : Speed test?
- Select from array only the best items
- ParallelQuery with methods that take in an IEnumerable
- System.NotSupportedException: Unable to create a constant value of type_.Only primitive and...this context
- LINQ to Entities does not recognize the method inside the select section
- How to apply a function to every element in a list using Linq in C# like the method reduce() in python?
- Expression.Equal - How to Compare Nullable and Non Nullable fields?
- Linq to Sql : Overwrite properties while Joining tables
- AmbiguousMatchException in Expression.PropertyOrField
- MVC, Linq - Sorting issue