Linq query 'Id in query' how to? - linq

Is there any tweek to use IN like query in lambda?
for example i have a query
Select * from Users where Id in ( 1,45,67, 89)
can i write the same in linq?
for example i have list of user say
List<Users> oUserList= new List<Users>();
and i have int list
List<Int32> Ids
and i want to write query like
var data= select all users from 'oUserList' where id not in 'Ids'
can any body tell me how to write this?
thanks
Issue solved
var data = oUserInfolist.Where(x => (!oo.Contains(x.ID)));

from u in oUserList where !Ids.Contains(u.UserID) select u

Related

Linq to join 2 tables and use contains for both tables

Ex:join master table with emp table and fetch empname from master table based on Id present in emp table and search if it contains specific string passed as parameter in master or search I'd passed in emp table.. It basically autocomplete search where u can search based on Id and also name
I want equivalent linq for below sql qwery
select app.employeeid, emp.employeename
from applicant app
join [EmployeeMaster] emp on app.employeeid = emp.EmployeeId
where app.employeeid like '%empid%' or emp.EmployeeName like '%empname%'
Can someone please help.
Welcome to the community Deepa,
You can use this query.
Please note that you cannot use like in the linq-to-sql, and you have to use Contains instead. Read this post for more information.
applicant
.Join(EmployeeMaster, app => app.employeeid, emp => emp.EmployeeId, (app, emp) => new { app, emp })
.Where(x => x.app.employeeid.Contains("1") || x.emp.Employeename.Contains("our"))
.Select(x => new
{
x.app.employeeid,
x.emp.Employeename,
});
By the way, if Applicant.employeeid is the same as EmployeeMaster.EmployeeId, then you don't need to join these two tables; unless your select or criteria clauses are different.
Try this:
var q = (from app in applicantList join emp in EmployeeMasterList on app.employeeid equals emp.EmployeeId where app.employeeid.Contains(empid) || emp.EmployeeName.Contains(empname) select new{ app.employeeid, emp.employeename }).ToList();

Parse.com linq relational query

Im using Xamarin.IOS and i want to run simple relations query using LINQ. I have to table. One table is NewSource other one is NewCategory. Two table relational with Name. For example :
NewSource table row:
Name: Radikal
Active: true
NewCategory table row:
NewSourceName: Radikal
Active:true
SportUrl: http://www.something.com
EconomyUrl= http://www.something.com
..
..
I wrote this query take from Parse document:
var query= from post in ParseObject.GetQuery("NewSource")
where (bool)post["Active"]==true //which mean i want to take only active New Source
select post;
var query2 = from comment in ParseObject.GetQuery("NewCategory")
join post in query on comment["NewSourcename"] equals post
select comment;
var comments = await query.FindAsync();
The code is not working. it returns always null. Where can i do wrong? I want to relational two table which connect is NewSource.Name and NewCategory.NewSourceName
How can i do this?
Thank you.
Assuming that NewSource table's Name column linked to NewSourceName column in NewCategory table, you can try to join them this way :
var query2 = from comment in ParseObject.GetQuery("NewCategory")
join post in query on (string)comment["NewSourcename"] equals (string)post["Name"]
select comment;

row number with help of linq

my table consists of 3 columns(sno,name,age) now i am retrieving this table from the database with extra column(row number) ,i used the following code
select * from (
select ROW_NUMBER() over (order by SNo asc)as rowindex,SNo,Name,Age
from tblExample)
as example where rowindex between ((pageindex*10)+1) and ((pageindex+1)*10)
note:here pageindex is the varaible that takes some intger value which is passed by the user
my data base is sql server 2008, now i want to write the same query using linq
can any one please change the abovesql query into linq. iam unable to do it as iam new to linq. iam struck up with this problem please help me thank you in advance
You can write query as beow
var index=1;
var pageIndex=1;
var pageSize = 10;
data.Select(x => new
{
RowIndex = index++,
Sno = x.Sno,
Name = x.Name,
Age = x.Age
}).OrderBy(x => x.Name)
.Skip(pageSize * (pageIndex - 1)).Take(pageSize).ToList();

SQL to LINQ query asp.net

I am currently trying to get some statistics for my website but i cant seem to create the query for my database to get the username that if found most frequent in all the rows.
The sql query should look something like this:
SELECT username FROM Views GROUP BY 'username' ORDER BY COUNT(*) DESC LIMIT 1
How do i make that query in my controller?
var username = db.Views.GroupBy(v => v.username).OrderByDescending(g => g.Count()).First().Key
(from a in Views
group a by a.username into b
let c = b.count()
orderby c descending
select a.username).take(1);
Your query conversion .....
This is how you do that query using LINQ:
var temp = (from a in Views
group a.username by a.username into b
orderby b.Count() descending
select b.Key).Take(1);
You can't do LIMIT 1 (mysql), since LinqToSql only generates TSql from MSSqlServer.

LINQ to DataSet, distinct by multiple columns

Just wanted to check if there is way to do distinct by multiple columns. Thanks in advance!!!
BTW, I found a great LINQ extension here but need some guidance to use it for multiple columns
Well, you can do the projection first:
var qry = db.Customers.Select(cust => new {cust.ID, cust.Name, cust.Region})
.Distinct();
Or in query syntax:
var qry = (from cust in db.Customers
select new {cust.ID, cust.Name, cust.Region}).Distinct();
That do?
Instead of Distinct you can use Groupby and then selecting the Top Most record of each group
How to LINQ Distinct by Multiple Fields without anonymous types
return from o in objEntity
group o by new
{
o.Field1,
o.Field2,
o.Field3,
o.Field4,
o.Field5
} into grp
select grp.FirstOrDefault();
This will give you the EntityObject Rather than the AnonymousType
By "distinct by multiple columns" what you really mean is a group by.
When you ask for distinct, it means that you are getting ALL the distinct rows, or, a group by using all the columns in the table.
If you want to only get distinct groupings for a subset of the columns, then use a group by in your clause, specifying the columns to group by. Then, select the groups, as you only want one set of keys for each group.
Another easy option is to create a single distinct string.
var result = collection.DistinctBy(c => c.Field1 + "." + c.Field2 + "." + c.Field3);
var qry = (from cust in db.Customers
select new {cust.ID, cust.Name, cust.Region}).GroupBy(x => new { x.Name,x.Region}).select(z => z.OrderBy(i => i.cust).FirstOrDefault()).ToList();

Resources