I got a List that contains all the employees, now I need to dig in to a specific employee on a new page. I want to get all the values from the employee where the ID is 1 for example. Is there a sollution for this in LINQ?
It's practically a Query SELECT * FROM Employee WHERE id = 1;
class Employee
{
public int EmployeeID { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public string Title { get; set; }
public string TitleOfCourtesy { get; set; }
public DateTime BirthDate { get; set; }
public DateTime HireDate { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string Region { get; set; }
public string PostalCode { get; set; }
public string Country { get; set; }
public string HomePhone { get; set; }
public string Extension { get; set; }
//public Image Photo { get; set; }
public string Notes { get; set; }
public int ReportsTo { get; set; }
public string PhotoPath { get; set; }
}
I tried it like this but it doesn't work:
List<Employee> employees = Database.getEmployees();
var uniqUsers = employees.Where(x => employees.Contains(x.EmployeeID == 1)).ToList();
Where employee is type of IEnumerable<Employee>
If you are expecting 1 record:
var result = employee.FirstOrDefault(x => x.EmployeeID == 1); // Returns Employee
If you are expecting more than 1 record:
var result = employee.Where(x => x.EmployeeID == 1); // Return IEnumerable<Employee>
Please note, when using FirstOrDefault if there is no items in your collection (or doesn't match your lambda) then it will return default T which in your case will be Employee and it will be null.
If you want a "single" item that meets that critera use the Single Linq statement:
Employee employee = employees.Single(e => e.EmployeeID == 1);
or
Employee employee = employees.SingleOrDefault(e => e.EmployeeID == 1);
if you want the query to return null instead of throwing an exception if there is not an item in the list that meets that criteria.
Let EmployeeList is the current List of Employees. You can use LINQ to filter the required details as like the specified query by using this(IT will give you all sublist satisfies the specified condition):
int empIdToSearch=1;
List<Employee> FilteredList=EmployeeList.Where(x=>x.EmployeeID ==empIdToSearch).ToList();
If the EmployeeID is unique then there will be one item in the list with particular ID, You can use FirstOrDefault to get the First item from the collection that satisfies the condition.ie.,
Employee EmployeeObject= FilteredList.FirstOrDefault(x => x.EmployeeID == empIdToSearch);
The concept that you need to get is how most linq queries operate.
When you say .Where(x => x.EmployeeID == 1) then x is a single empolyee as if you said:
foreach(Employee x in employees)
{
if(x.EmployeeID == 1)
// take it
}
So the correct syntax would be:
List<Employee> uniqUsers = employees.Where(x => x.EmployeeID == 1).ToList();
Single Optional Result:
Employee uniqUser = employees.SingleOrDefault(x => x.EmployeeID == 1);
Single Mandatory Result:
Employee uniqUser = employees.Single(x => x.EmployeeID == 1);
First Optional Result:
Employee uniqUser = employees.FirstOrDefault(x => x.EmployeeID == 1);
First Mandatory Result:
Employee uniqUser = employees.First(x => x.EmployeeID == 1);
We can fetch the records from collection in two ways.
Linq to sql like query
var employee= from emp in employees where emp.ID==1;
Linq to extension methods.
var employee = employees.Where(emp=>emp.ID==1);
Linq supports a query syntax that is closer to SQL.
var employee1 = from employee in employees where employee.EmployeeID == 1 select employee;
foreach (var x in employee1)
{
Console.WriteLine(x.EmployeeID);
}
The compiler converts all query syntax to method syntax. Not all things can be done with query syntax. The 'from' comes before the 'select' so auto-complete is more useful. It is important to note the linq query is not executed until it is used. The foreach loop is where it is first used in this example.
Related
This query gets all the rows from the join table, TagRecipes (many-to-many), where the TagId is found in a list, tagIdList, and lastly just returns the Recipe. How can I make it so it only returns Recipes that have all the tags in the list, tagIdList?
Basically, it's filtering by 'or', but I want it to filter by 'and'. The recipe must contain ALL the tags, not just some.
var allRecipes = await _context.TagRecipes
.Where(tr => tagIdList.Contains(tr.TagId))
.Select(i => i.Recipe).Distinct()
.ToListAsync();
e.g, tagIdList = {17, 20 ,21 }
TagRecipes
So, it should only return Recipe with RecipeId = 2, even though RecipeID 4 contains TagId 17
Classes
public class Recipe
{
public int Id { get; set; }
public string Name { get; set; }
public string Ingredients { get; set; }
public string Instructions { get; set; }
public string ImageURL { get; set; }
public string Description { get; set; }
public ICollection<TagRecipe> TagRecipes { get; set; }
public virtual ICollection<StarRating> StarRatings { get; set; }
public ICollection<Binder> Binders { get; set; }
}
public class TagRecipe
{
public int TagId { get; set; }
public int RecipeId { get; set; }
public Tag Tag { get; set; }
public Recipe Recipe { get; set; }
}
Thank you
group TagRecipes by RecipId, so each RecipId (Key) has its own tagIds.
loop on each group to check if it has all the tags in the provided tagIdList, and if it has them all, store this RecipId (Key), in my case i created list of int RecipIds.
get all the Recipes in the RecipIds list.
I hope this could be helpful
List<int> RecipIds = new List<int>();
int count = 0;
var RecipGroup = _context.TagRecipes.GroupBy(tr => tr.RecipeId);
foreach (var group in RecipGroup)
{
count = 0;
foreach (var tr in group)
{
if (tagIdList.Contains(tr.TagId))
{
count += 1;
}
}
if (tagIdList.Length == count)
{
RecipIds.Add(group.Key);
}
}
var allRecipes = _context.Recipes.Where(r => RecipIds.Contains(r.Id)).ToList();
I believe the following solution using Linqkit will be the simplest way to solve this, and without returning duplicates.
var tagIdList = new List<int> {1, 2};
var predicate = tagIdList.Aggregate(PredicateBuilder.New<Recipe>(), (pred, currentTagId) =>
pred.And(recipe => recipe.TagRecipes.Any(x => x.TagId == currentTagId)));
var result = _context.Recipes.Where(predicate).ToList();
Generates this SQL:
SELECT "r"."Id", "r"."Name"
FROM "Recipes" AS "r"
WHERE EXISTS (
SELECT 1
FROM "TagRecipes" AS "t"
WHERE ("r"."Id" = "t"."RecipeId") AND ("t"."TagId" = #__currentTagId_0)) AND EXISTS (
SELECT 1
FROM "TagRecipes" AS "t0"
WHERE ("r"."Id" = "t0"."RecipeId") AND ("t0"."TagId" = #__currentTagId_1))
Code is tested and verified using an asp.net Core 5 app.
Basically, it's filtering by 'or', but I want it to filter by 'and'.
The recipe must contain ALL the tags, not just some.
So, it should only return Recipe with RecipeId = 2, even though RecipeID 4 contains
TagId 17
Since you want to filter the data by and, after filtering the data based on the tag, if group the result based on the RecipeId property, the item count in the group should be the equally with the tag list count. So, you could use the following query statement:
var tagIdList = new List<int>() { 17, 20, 21 };
var allRecipes = _context.TagRecipes.Include(tr => tr.Recipe)
.Where(tr => tagIdList.Contains(tr.TagId))
.ToList()
.GroupBy(tr=> tr.RecipeId)
.Where(c => c.Count() == tagIdList.Count)
.Select(i => new { RecipeId = i.Key, Count = i.Count(), Recipe = i.FirstOrDefault().Recipe })
.ToList();
The result as below:
I have two objects: PhraseCategory and Phrase. Here's the classes:
public class PhraseCategory
{
public System.Guid PhraseCategoryId { get; set; } // PhraseCategoryId
public int PhraseCategoryShortId { get; set; } // PhraseCategoryShortId (Primary key)
public int PhraseCategoryGroupId { get; set; } // PhraseCategoryGroupId
public string Name { get; set; } // Name (length: 20)
// Reverse navigation
public virtual System.Collections.Generic.ICollection<Phrase> Phrases { get; set; } // Phrase.FK_PhrasePhraseCategory
}
public class Phrase : AuditableTable
{
public System.Guid PhraseId { get; set; } // PhraseId (Primary key)
public string English { get; set; } // English
public int? CategoryId { get; set; } // CategoryId
// Foreign keys
public virtual PhraseCategory PhraseCategory { get; set; } // FK_PhrasePhraseCategory
}
Can someone tell me how I could join these so that I am able to select all the phrases with for example a PhraseCategoryGroupId of 25.
Here's what I have right now but it does not take into account my need to also be able to select the Phrases with a PhraseCategory that has a PhraseCategoryGroupId:
List<Phrase> phrases;
var query = db.Phrases.AsQueryable();
if (options.CreatedBy != 0) query = query
.Where(w => w.CreatedBy == options.CreatedBy);
phrases = await query
.AsNoTracking()
.ToListAsync();
return Ok(phrases);
Note that I would like to get just a flat output (hope that makes sense). What I mean is a list that contains just:
PhraseId, English and CategoryId
This should get you what you need:
phrases = phrases.Where( x => x.PhraseCategory.PhraseCategoryGroupId == 25 )
.Select( x => new
{
PhraseId = x.PhraseId,
English = x.English,
CategoryId = x.CategoryId
});
Please note that you can also create instances of another type instead of the anonymous type which I am creating in the above query.
Also, the PhraseCategory will be lazy loaded in the above query since you have lazy loading enabled on the property: it is virtual. If you have lazy loading disabled globally, then you will need to use the Include method in your query. Then your query will become:
phrases = phrases.Include(x => x.PhraseCategory)
.Where( x => x.PhraseCategory.PhraseCategoryGroupId == 25 )
.Select( x => new
{
PhraseId = x.PhraseId,
English = x.English,
CategoryId = x.CategoryId
});
I would like to select a where statement that adds items to a list where only product codes match. I have it so it gets all of the products sold in the sale but I would like there were statement to get only products in this sale.
PS: This is really hard to explain
Model
public class userSales
{
public string Name { get; set; }
public string UserName { get; set; }
public int Sale_Id { get; set; }
public int CostumerID { get; set; }
public string Sale_Date { get; set; }
public string Paid { get; set; }
public Nullable<int> Sale_Cost { get; set; }
public string Discount_Code { get; set; }
public List<SaleProduct> saleProductsList { get; set; }
}
public class SaleProduct
{
public int SaleID { get; set; }
public string ProductCode { get; set; }
public int ProductCount { get; set; }
public string Image_Path { get; set; }
public string Shoot_Date { get; set; }
public string Shoot_Info { get; set; }
}
Linq statement where I'm having trouble:
var test = (from _ClientData in db.ClientDatas
join _salesInfo in db.Sales_Infoes
on _ClientData.CostumerID
equals _salesInfo.CostumerID
where _ClientData.UserName == _userName
select new userSales()
{
CostumerID = _ClientData.CostumerID,
Name = _ClientData.Name,
UserName = _ClientData.UserName,
Sale_Id = _salesInfo.Sale_Id, // This is the item i would like to use in my were statement
Sale_Date = _salesInfo.Sale_Date,
Sale_Cost = _salesInfo.Sale_Cost,
Discount_Code = _salesInfo.Discount_Code,
Paid = _salesInfo.Paid,
// Problem here
saleProductsList = db.SaleProducts.Where()
}).ToList();
Got to this based on the answer:
var reult = db.ClientDatas.Where(a => a.UserName == _userName)
.Join(db.Sales_Infoes,
a => a.CostumerID,
b => b.CostumerID,
(a, b) => new userSales()
{
CostumerID = a.CostumerID,
Discount_Code = b.Discount_Code,
Sale_Cost = b.Sale_Cost,
Sale_Id= b.Sale_Id,
Name = a.Name,
Sale_Date = b.Sale_Date,
UserName = a.UserName,
Paid = b.Paid,
saleProductsList = db.SaleProducts.Where(c => c.SaleID == b.Sale_Id).ToList()
}).ToList();
You're not looking for a where, you're looking for a join. Where filters the results on a single table, join intersects two tables which is actually what you want here.
var result = db.Sales_Infoes.Where(x => x.UserName == _userName)
.Join(db.ClientDatas,
x => x.Sale_Id,
y => y.Sale_id,
(x, y) => new userSales() {
// x is SalesInfo obj y is ClientDatas obj do assignement here
Name = y.Name,
Sale_Date = y.Sale_date
}).ToList();
Just fyi I haven't had a chance to test that but it's the basic idea. You don't need a select like in your statement because the last argument I'm passing into join is the lambda (x, y) => ... in that case x and y are the current row from each table (that we've gotten from applying our where to the user sales table then joining those results into the salesproduct table) so whatever projections you want to do occur there. The other two method args above that are the telling join which fields to compare, it's the 'key selector' lambda expression for each table.
I am using RavenDb in C# web project. I have an object that I need to query its child collection with 1 row per child object and some of the root/parent object properties.
Note: This is not the actual design, just simplified for this question.
public class OrderLine
{
public string ProductName { get; set; }
public int Quantity { get; set; }
public DateTime? ShipDate { get; set; }
}
public class Order
{
public int OrderId { get; set; }
public string CustomerName { get; set; }
public DateTime OrderDate { get; set; }
public List<OrderLine> OrderLines { get; set; }
}
The order with the orderlines is one single document. ShipDate will be updated on each line because not all products are always in stock.
I need to be able to create a list of the last 10 products sent with the following columns:
OrderId
Customer
ProductName
ShipDate
This doesn't work because SelectMany is not supported:
var query = from helper in RavenSession.Query<Order>()
.SelectMany(l => l.OrderLines, (order, orderline) =>
new { order, orderline })
select new
{
helper.order.OrderId,
helper.order.CustomerName,
helper.orderline.ProductName,
helper.orderline.ShipDate
};
var result = query.Where(x => x.ShipDate.HasValue)
.OrderByDescending(x => x.ShipDate.Value).Take(10);
I believe the right thing to do isto create an Index that will flatten out the list but I haven't had any success. I don't believe a Map-Reduce situation will work because as I understand it will effectively does a group by which Reduces the number of documents to less rows (in the index). But in this case, I am trying to expand the number of documents to more rows (in the index).
I would rather not put each OrderLine in a separate document but I do not know what my options are.
Since you want to filter and sort by fields in the subclass, you'll need to make sure all the fields you want are indexed and stored.
public class ShippedItemsIndex
: AbstractIndexCreationTask<Order, ShippedItemsIndex.Result>
{
public class Result
{
public int OrderId { get; set; }
public string CustomerName { get; set; }
public string ProductName { get; set; }
public int Quantity { get; set; }
public DateTime ShipDate { get; set; }
}
public ShippedItemsIndex()
{
Map = orders =>
from order in orders
from line in order.OrderLines
where line.ShipDate != null
select new
{
order.OrderId,
order.CustomerName,
line.ProductName,
line.Quantity,
line.ShipDate
};
StoreAllFields(FieldStorage.Yes);
}
}
Then you can project from the index into your results.
var query = session.Query<Order, ShippedItemsIndex>()
.ProjectFromIndexFieldsInto<ShippedItemsIndex.Result>()
.OrderByDescending(x => x.ShipDate)
.Take(10);
var results = query.ToList();
Here is a complete test demonstrating.
I have a List<Order>
public int OrderID { get; set; }
public string CustID { get; set; }
public string Details { get; set; }
I want to write a method that accepts a ID, then searches this List for matching records that have same CustID and returns ORderID and Details in a List<>
This will get a sequence of Order objects that match the criteria:
var ordersIWant = myList.Where(order => order.CustID == "some customer ID");
public List<Order> Get(string id)
{
List<Order> orders = new List<Order>(); // pass this in as a param or globally refer to it
var query = from o in orders
where o.CustID == id
select o;
return query.ToList();
}
Or if you want to specifically return only those two fields maybe something like:
public class Order : IOrderDetails
{
public int OrderID { get; set; }
public string CustID { get; set; }
public string Details { get; set; }
}
public interface IOrderDetails
{
int OrderID { get; set; }
string Details { get; set; }
}
public List<IOrderDetails> Get(string id)
{
List<Order> orders = new List<Order>(); // pass this in as a param or globally refer to it
var query = from o in orders
where o.CustID == id
select o as IOrderDetails;
return query.ToList();
}
Assuming those properties you listed belong to a class.
string searchId="15";
var list = (from item in myList
where item.OrderId == searchId
select new {OrderId= item.OrderId,Details = item.Details }).ToList();
Just wrote that without compiling... good luck.
Since you only wanted OrderID and Details I returned an anonymous object. Could also just return item.