Table users:
userid username
------------------------------------
1 venkatesh duggirala
2 deviprasad
3 dhanu
if user sends username="d" then need to get all records.by using "contains" i am getting 2,3 as result.but 1st record also having "d" in duggirala.
query:
var result = from p in cxt.users
where p.Users.username.Contains(name)
select new
{
p.Userid
};
rewrite your query like this:
var result = from p in cxt.users
where p.Users.username.ToLower().Contains(name.ToLower())
select new
{
p.Userid
};
Related
I need to count how many records in the tableA are not in the tableA, how to do this with LINQ?
with SQL I do the following way
select count(*) as total from produtoitemgrade g
where g.id not in (select idprodutograde from produtoestoque where idProduto = 12)
and g.idProduto = 12
my linq code so far.
var temp = (from a in Produtoitemgrades
join b in Produtoestoques on a.IdUnico equals b.IdUnicoGrade into g1
where g1.Count(y => y.IdProduto == 12)>0 && !g1.Any()
select a).ToList();
I tried to follow that example LINQ get rows from a table that don't exist in another table when using group by?
but an error occurs when running, how can I do this?
Thanks!
Your query should looks like the following, if you want to have the same SQL execution plan:
var query =
from a in Produtoitemgrades
where !Produtoestoques.Where(b => a.IdUnico == b.IdUnicoGrade && b.idProduto == 12).Any()
&& a.idProduto == 12
select a;
var result = query.Count();
I have some data, similar to this
Email Date Id
Anne 1/1/00 1
Anne 1/2/00 2
Anne 1/4/00 3
Bert 1/4/00 4
Bert 1/5/00 5
I'm trying to return the following 2 results from the above into something like
List<Model, Count>
Where Model is a class with Email and Date (most recent Date) properties, and count is the total count
var m = (from e in emailOpens
group e by e.EmailAddress into g
select new
{
Model = g.Key,
Occurance = g.Count()
}).ToList();
The issue is, the above will return only the email addresses but I change the group statement to
group e by e into g
Then naturally there is nothing to count, so my Occurrence property remains at 0
What am I missing here?
You are missing just one statement:
var m = (from e in emailOpens
group e by e.EmailAddress into g
select new
{
Model = new
{
Email = g.Key,
// Select maximum date from group
MostRecentDate = g.Max(m => m.Date)
},
Occurance = g.Count()
}).ToList();
I am trying to flatten out my webapi EF using a DTO and linq but not quite sure how to do it. My end goal is to say, I want to return ALL four accounts where USERNAME = x.
In this example there is 1 username with access to 1 client, and that 1 client has 4 accounts.
How can I make the result come back with 4 entries, 1 for each account?
Here is what I have so far...
var x2 = from b in db.AspNetUsers
where b.UserName == username
select new AspNetUserDetailDTO()
{
UserName = b.UserName,
Email = b.Email,
Mapping_UserClient = b.Mapping_UserClient
//ClientName = b.Mapping_UserClient.SelectMany<Mapping_UserClient>(x => x.ClientID)
//Mapping_UserClient = b.Mapping_UserClient
};
below is my sql diagram.
so I tried writing a plain sql query to return a basic result of what I am looking for... now I do not know how to do this in LINQ
SELECT
dbo.Clients.ClientName
,dbo.Mapping_UserClient.ClientID
,*
FROM [xxx].[dbo].[AspNetUsers]
inner join dbo.Mapping_UserClient on dbo.Mapping_UserClient.AspNetUsersID = dbo.AspNetUsers.Id
inner join dbo.Clients on dbo.Clients.ClientID = dbo.Mapping_UserClient.ClientID
inner join dbo.Mapping_ClientAccount on dbo.Mapping_ClientAccount.ClientID = dbo.Clients.ClientID
inner join dbo.Accounts on dbo.Accounts.AccountID = dbo.Mapping_ClientAccount.AccountID
where Email = 'dddd'
Translating your query above, you would get something like this in Linq:
var query = (from acc in db.Accounts
join mca in db.Mapping_ClientAccount
on acc.AccountId equals mca.ClientID
join cli in db.Clients
on mca.ClientID equals cli.ClientID
join muc in db.Mapping_UserClient
on cli.ClientID equals muc.ClientID
join anu in db.AspNetUsers
on muc.AspNetUsersID equals anu.Id
where anu.UserName == username
select new AspNetUserDetailsDTO()
{
ClientName = cli.ClientName,
ClientID = cli.ClientID
}).ToList();
Consider this query:
from e in db.MyEntities
join o in db.MyOtherEntities
on new e.Foo equals o.Foo into others
from o in others.DefaultIfEmpty()
select new
{
Value1 = e.Value1,
Value2 = o.Value2
};
With this simple left join, Entity Framework fails on doing the following group
from e in query group e by new { } into g select g.Count()
That may seem obscure, but it's actually a common thing for automatic grid implementations to do with your queries.
I encountered this using DevExtreme's data library: Total summaries wouldn't work on queries with left joins.
What you get is a
NotSupportedException: The nested query is not supported. Operation1='GroupBy' Operation2='MultiStreamNest'
This works though:
from e in query.Take(1) select { Count = query.Count(), /* other aggregates */ }
And so does this:
from e in query group e by e.SomePropertyThatsActuallyConstant into g select g.Count()
There is a workaround. You can write your query like this:
from e in db.MyEntities
from o in db.MyOtherEntities.Where(o => o.Foo == e.Foo).DefaultIfEmpty()
select new
{
Value1 = e.Value1,
Value2 = o.Value2
};
Strangely it also works when you put a where clause after the join:
from e in db.MyEntities
join o in db.MyOtherEntities
on new e.Foo equals o.Foo into others
from o in others.DefaultIfEmpty()
where e.Value1 == e.Value1
select new
{
Value1 = e.Value1,
Value2 = o.Value2
};
The where clause condition must not be merely constants, I guess EF is smart enough to reduce it otherwise.
Table1 :
userid name address
1 venkat srinagr
2 venkatesh sainagar
Table2:
id userid lat lon
1 1 14.000 15.000
2 2 14.3526 15.3698
by passing "venkat" as parameter then need to pull all matching records and his userid,name,lat,lon.
in above table1 "venkat" contains in both rows then need to pull 2 records.how to get userid,name,lat,lon for all matching rows..
for sigle record i am able to get.but there are multiple rows how to get please tell me....
var result = from p in cxt.Table2
where p.Table1.Name.Contains(name)
select new
{
p.Users.User_Id,p.Users.Name,p.Latitude,p.Longitude
};
Im sure someone will say this is not the most effective way but this is how i would do it.
string InputString = "venkat";
var tab =(from a in db.tablea
from b in db.tableb
where a.userid == b.userid && a.name == InputString
select new
{
UserID = a.userid,
Username = a.name,
Latitude = b.lat,
Longditude = b.lon
}).FirstOrDefault();
FirstOrDefault() is only if you want to force only one output or null,
if you want a collection of some sort, then just remove it.