Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I developed a search functionality recently which searches database on EnglishValue column in Tag table. I recieve list of Keywords from search box and i search it using following query and it gives me correct results.
List<string> keywords = new List<String>(searchText);
IQueryable<Tag> tags = from p in db.Tags
where keywords.Contains(p.EnglishValue)
select p;
var matchingTagList = tags.ToList();
Now one of the ask is to search using "AND", currently LINQ query is doing OR. If any one can give suggestions how to do AND search using LINQToSQL that will help.
I am adding more details on how my database looks like. If i seach for Pet-Friendly and Wi-Fi than i should get Id= 1.
Try this:
List<string> keywords = new List<String>(searchText);
IQueryable<Tag> tags = from p in db.Tags
where keywords.All(x=> p.EnglishValue.Contains(x))
select p;
var matchingTagList = tags.ToList();
Simply try this. That why I love LINQ. Assuming you keyword is containing the term and also some varialble here called Num =5;
List<string> keywords = new List<String>(searchText);
IQueryable<Tag> tags = from p in db.Tags
where keywords.Contains(p.EnglishValue) && p.num==5
select p;
var matchingTagList = tags.ToList();
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have this code
var value = (from dc in _context.ContractDetails
where dc.EmployeeID == id
select dc.Amount);
return value;
}
is it acceptable to do Value.Sum();
You want to return the sum it looks like. Instead of having query be a decimal, just let it be what it wants (var, it's really IEnumerable<decimal>). Then you can return an aggregate on that. Sum for example
var query = from emp in Employees
join cd in ContractDetails
on emp.EmployeeID equals cd.EmployeeID
where cd.EmployeeID == id
select cd.Amount;
return query.Sum();
If this is all it does, then I also feel like you don't need to join at all, and it would be simpler to do
var query = from cd in ContractDetails
where cd.EmployeeID == id
select cd.Amount;
return query.Sum();
... unless you were using the join to test for the existence of an employee in the Employee table as a condition.
Your linq statement results in an IQueryable<Amount>, you would need to take that result and call Sum() on it to get the result you're seeking.
First, isn't there a navigation property you can use (i.e. Employee.ContracteDetails) instead of manually joining the two sets? For example,
var sum = _context.Employee
.Where( e => e.Id == id )
.Select( e => e.ContractDetails.Sum( cd => cd.Amount ) )
.SingleOrDefault();
Second, you're not using any information you need from Employee, even your where clause references ContractDetails alone; why start your query there? Work with _context.ContractDetails instead:
var sum = _context.ContractDetails
.Where( cd => cd.EmployeeId == id )
.Sum( cd => cd.Amount );
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
so i have this query in sql:
select (select ConfigItemDescripcion from SGRC_ConfigItem where ConfigId = 'SEGM' and ConfigItemId = SegmentoId) Segmento,
(select ConfigItemDescripcion from SGRC_ConfigItem where ConfigId = 'MRCA' and ConfigItemId = MarcaId) Marca,
Producto,
Familia
from sgrc_emisor
where EmisorCuenta = '3702406435'
I want to write the same query in a linq expression or a lambda expression.
Thanks for the help in advance
Finally i manage to come up with the query in linq, dont know how to do it in lambda, but it works fine.
var obj = (from emisor in _context.DbSetEmisores
where emisor.EmisorCuenta == cuenta
select new EmisorDto
{
Segmento =
((from itemConf in _context.ItemsDeConfiguracion
where itemConf.ConfigID == "SEGM" && itemConf.ConfigItemID == emisor.SegmentoId
select new { itemConf.ConfigItemDescripcion }).FirstOrDefault().ConfigItemDescripcion),
Marca =
((from itemConf in _context.ItemsDeConfiguracion
where itemConf.ConfigID == "MRCA" && itemConf.ConfigItemID == emisor.MarcaId
select new { itemConf.ConfigItemDescripcion }).FirstOrDefault().ConfigItemDescripcion),
Producto = emisor.Producto,
Familia = emisor.Familia,
SegmentoId = emisor.SegmentoId,
MarcaId = emisor.MarcaId,
}).FirstOrDefault();
When using LINQ you can use either Query syntax as shown in the LINQ below (If you are familiar with SQL then this looks more natural).
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/query-syntax-and-method-syntax-in-linq
The other option is to use Method syntax, and below is a short example. This allows for chaining of methods, biggest thing to keep in mind is "var" should be used, the return type is dynamic and the compiler will help you out a lot if you just use "var"
var items = _list.Where(x => x.Attribute1 == "NextField")
.Where(x => x.Attribute2 == "Something else");
Other things that hangs folks up sometimes is LINQ uses "delayed execution"
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
How to convert normal SQL to LINQ query? Is there tools that can do that?? Online Tools
Have you tried something like this (a few joins omitted for brevity):
var result = from s in TooltipsLanguage
join c in TooltipsLanguageSection on s.Id equals c.IdLanguage
join p in TooltipsSection on c.IdSection equals p.Id
join ...
select new MyDestinationObject()
{
Id = s.BusinessEntityID,
Language = s.Language,
IdLanguage = c.IdLanguage,
...
};
This meight be help you.
var temp= edbContext.TooltipsLanguage.select(
c=> new {
TooltipsLanguage.Id,
TooltipsLanguage.Language,
TooltipsLanguage.TooltipsLanguageSection.Id,
TooltipsLanguage.TooltipsLanguageSection.IdLanguage,
TooltipsLanguage.TooltipsLanguageSection.IdSection,
TooltipsLanguage.TooltipsLanguageSection.TooltipsSection.Id,
TooltipsLanguage.TooltipsLanguageSection.TooltipsSection.Section,
TooltipsLanguage.TooltipsLanguageSection.TooltipsSection.Section.TooltipsItem.Id,
TooltipsLanguage.TooltipsLanguageSection.TooltipsSection.Section.TooltipsItem.IdItem,
TooltipsLanguage.TooltipsLanguageSection.TooltipsSection.Section.TooltipsItem.TooltipsItemText.Id,
TooltipsLanguage.TooltipsLanguageSection.TooltipsSection.Section.TooltipsItem.TooltipsItemText.IdItem,
TooltipsLanguage.TooltipsLanguageSection.TooltipsSection.Section.TooltipsItem.TooltipsItemText.IdText,
TooltipsLanguage.TooltipsLanguageSection.TooltipsSection.Section.TooltipsItem.TooltipsItemText.TooltipsText.Id,
TooltipsLanguage.TooltipsLanguageSection.TooltipsSection.Section.TooltipsItem.TooltipsItemText.TooltipsText.Texts});
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am trying to retrieve role info as below:
var allRoles = (from roles in context.aspnet_Roles
select new { roles.RoleId, roles.RoleName }).ToList();
I want to store these role data to a specific class type or LIST<string> not to object of type var like allRoles.
You are currently selecting a list of anonymous type. What you seem to want is just the name and as such you should select just the name as a result instead of projecting a new object.
var allRoles = (from roles in context.aspnet_Roles
select roles.RoleName).ToList();
To change the things stored in the list, you change the bit after the select keyword. For example:
var allRoles = (from roles in context.aspnet_Roles
select "ID=" + roles.RoleId + ";Name=" + roles.RoleName).ToList();
The specific expression following the keyword will depend on the string format you want for the list's elements.
By the way, this code does not have any lambda expressions in it.
What I understand from these details is that there are a few columns in "context.aspnet_Roles" but, you only want to use RoleId and RoleName.
Here is what I would do :
List<MagicClass> allRoles= new List<MagicClass>();
allRoles= (from roles in context.aspnet_Roles
select new MagicClass
{
RoleId = roles.RoleId,
RoleName = roles.RoleName
}).ToList();
internal class MagicClass
{
private string RoleId {get;set;}
private string RoleName {get;set;}
};
Hope this helps.
I would suggest if you want to store role name only use this
var allRoles = (from roles in context.aspnet_Roles
select new { roles.RoleName }).ToList();
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Ok so I have these tables:
Video
Id
Title
VideoTag
Id
VideoId
TagId
Tag
Id
Name
I need a LINQ to SQL query to perform a search on the Videos, either by the title or by the tags.
For the moment all I have is this for searching by the title:
IQueryable<Video> videos =
from v in ctx.Videos
where v.Title.Contains(SearchString)
select
new Video
{
Id = v.Id,
Title = v.Title
};
Try this:
from v in ctx.Videos
join vt in ctx.VideoTags on v.Id equals vt.VideoId
join t in ctx.Tag on vt.TagId equals t.Id
where v.Title.Contains(SearchString) || t.Name.Contains(SearchString)
select
new Video
{
Id = v.Id,
Title = v.Title
};