Linq not contains dynamic query [duplicate] - c#

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Linq to SQL “not like” operator
How can I write a dynamic linq query with using not contains?
I use .Contains() instead of like. But what should I use instead of not like?

just use ! before the contains condition. Like
var myProducts = from p in products
where !productList.Contains(p.ID)
select p;

Some thing like this should help...
YourDataContext dc = new YourDataContext();
var query =
from c in dc.Customers
where !(from o in dc.Orders
select o.CustomerID)
.Contains(c.CustomerID)
select c;

Use ! operator. Like this:
private List<int> iList = new List<int>
{
1,2,3,4,5,6,7,8,9
};
if (!iList.Contains(888))
{
}

Related

c# linq Query how to write multiple And condition [duplicate]

This question already has answers here:
How to do joins in LINQ on multiple fields in single join
(13 answers)
Closed 2 years ago.
This is My Query in Linq
VAR x = FROM n IN _db.tbl_maint_activity_material_details
JOIN availablity IN _db.tbl_maint_item_availability ON n.mamd_lotno equals availablity.mia_lot_no && n. ////
Here im not able to countine my Code Please Help me
This is My SQL query
SELECT *
FROM tbl_maint_activity_material_details AS _material
JOIN tbl_maint_item_availability AS availablity
ON _material.mamd_maint_item_id = availablity.mia_maint_item_id
AND _material.mamd_lotno = availablity.mia_lot_no
WHERE _material.mamd_status = 24
To accomplish that, you have to build an object and use equals.
Something like:
on new { mia_lot_no = n.mamd_lotno, mamd_lotno = n.mamd_lotno } equals new { mia_lot_no = availablity.mia_lot_no, mamd_lotno = ... }
So that the equals will result true if the whole object is equal.

LINQ Multiple order by when not using lambda expressions [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Multiple “order by” in LINQ
I want to order some dataset by some field AND THEN some other field.
Using lambda expressions this would be easy (OrderBy.ThenBy), but how to do it when I have a syntax like this:
int maxQueries = int.MaxValue;
// finds the most search for queries
var ordered = from p in searchLogs
where !string.IsNullOrEmpty(p.SearchQuery)
group p by new
{
SearchQuery = p.SearchQuery
}
into pgroup
let count = pgroup.Count()
orderby count descending
select new
{
Count = count,
SearchQuery = pgroup.Key.SearchQuery
};
I can't seem to find a way which works after the decending keyword (like orderby count descending then searchquery)..
Any ideas?
Put a comma after descending and specify the next thing to sort by (optionally adding descending) and so on
Just to add some code to mlorbetske answer, if you have Customer class like this:
public class Customer
{
public string Name;
public DateTime MemberSince;
}
You could then perform an ordering like this:
var q = from c in Customers
orderby c.MemberSince.Date descending, c.Name
select c;

LIKE query with Entity Framework [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to do SQL Like % in Linq?
Like Operator in Entity Framework?
I'm doing a query like this:
var matches = from m in db.Customers
where m.Name == key
select m;
But I don't need m.Name to be exactly equal to key. I need m.Name to be like key.
I can't find how to recreate the SQL query:
WHERE m.Name LIKE key
I'm using SQL Server 2008 R2.
How to do it?
Thanks.
Would something like this linq query work for you.. ?
var matches = from m in db.Customers
where m.Name.Contains(key)
select m;
this should also work I edited my answer.
Contains is mapped to LIKE '%#p0%' which is case insensitive
var matches = from m in db.Customers
where m.Name.StartsWith(key)
select m;
Make the search and compare whether the string is either lowercase or uppercase to get the best result since C# is case-sensitive.
var matches = from m in db.Customers
where m.Name.ToLower().StartsWith(key.ToLower())
select m;

Linq to SQL - Query

I am trying to mimic below statement in Linq to SQL.
WHERE (rtrim(posid) like '%101' or rtrim(posid) like '%532')
I statement basically determine if posid ends with 101 or 532. In the above example I am only making 2 comparisons but their could be 1 to N comparisons all joined with OR. I store the comparison values (101,532,...) in a generic list that I send to my Linq to SQL method.
I have tried to mimic above SQL using a where clause unsuccessfully (example below):
var PosNum = new List<string>();
PosNum.Add("101");
PosNum.Add("532");
var q = (from a in context.tbl_sspos select a);
q = q.Where(p => PosNum.Contains(p.posid.Trim()));
The issue with the above where clause is that it tries to do an exact match rather I want an ends with comparison.
How would I mimic the SQL statement in Linq to SQL.
Thank You in advance for any help / advice you can provide.
I would use String.EndsWith();
This will check the end of the string rather than entire contents of it.
var q = (from a in context.tbl_sspos select a);
q = q.Where(p => p.posid.EndsWith("102") || p.posid.EndsWith("532"));
In EF 4 you can use the StartsWith / EndsWith methods by now. Might also work in LINQ to SQL.
UPDATE
Just realized that you are trying todo this against multiple values (PosNum), I don't think that this is directly supported currently. You can however concatenate multiple Where()clauses to get the result.
UPDATE 2
As AdamKing pointed out concatenating the where clauses was filtering against all PosNum values, here is the corrected version:
var baseQuery = (from a in context.tbl_sspos select a);
IEnumerable<YourType> q = null;
foreach(var pos in PosNum)
{
if(q == null)
q = baseQuery.Where(a => a.posid.EndsWith(pos));
else
q = q.Union(baseQuery.Where(a => a.posid.EndsWith(pos)));
}
This is not as pretty anymore, but works nonetheless.

Linq query with multiple where's [duplicate]

This question already has answers here:
Multiple WHERE clause in Linq
(3 answers)
Closed 2 years ago.
I am trying the to query my Status Update repository using the following
var result = (from s in _dataContext.StatusUpdates
where s.Username == "friend1" && s.Username == "friend2" etc...
select s).ToList();
Instead of using s.Username == "friendN" continuously is there anyway I can pass a list or array or something like that rather that specifying each one, or can I use a foreach loop in the middle of the query.
Thanks
If you only need to check whether the Username property has some specified value, you can create a list of the values and then use method such as All or Any to check if some condition holds for any/all elements of the array.
Your example looks a bit suspicious though - the user name s.Username cannot be equal to multiple different strings. Did you want to check whether it is equal to any of the (specified) names? That could be written like this:
var friends = new[] { "friend1", "friend2", ... };
var result =
from s in dc.StatusUpdates
where friends.Any(fr => s.Username == fr)
select s;
This returns all status updates such that the Username property is equal to any of the specified friend names (specified as an array, but you could use any IEnumerable<string>).
Yo could do it like this:
IQueryable<s> query= _dataContext.StatusUpdates;
foreach (var item in names)
{
query = query.Where(p=>p.Username == item);
}
List<s> result = query.ToList();
I think I mucked with some data types of yours but this should be close:
var names = new List<string>();
// populate names
var updates = new List<StatusUpdate>();
// populate updates
var result = (from s in updates
where names.Contains(s.ToString())
select s).ToList();

Categories