How to use a union using linq? [duplicate] - c#

This question already has answers here:
Linq union usage?
(3 answers)
Closed 4 years ago.
I have two tables:
1) Customer Table
2) Custmer Email Table
what i want to get both email ids which have customerId=4.
can I make a union and get my desire result like:
**
cusromerId:4
EmailId: sumit.printzone#gmail.com
vinod.printzone#gmail.com
**

In lambda syntax this is what you want:
var emails = firstTable
.Where(row => row.Id == 4)
.Select(row => row.Email)
.Union(secondTable
.Where(row => row.Id == 4)
.Select(row => row.Email));

Related

Using orderby with Select() in lambda expression C# [duplicate]

This question already has answers here:
C# list.Orderby descending
(6 answers)
Closed 19 days ago.
I have a variable which have 4 properties. How can I order this by Ids and then using this order for selecting ProductIds.
var products = await _festivalService.GetFestivalProductByProductIds(
searchProductIds, searchModel.FestivalId);
var productIds = products.Select(x => x.ProductId).ToArray();
productIds Should be sorted descending base on Ids and then select productIds.
what about OrderByDescending befor Select?
var productIds = products.OrderByDescending(x => x.ProductId)
.Select(x => x.ProductId).ToArray();

Can you use grouping in Entity Framework [duplicate]

This question already has answers here:
SQL to Entity Framework Count Group-By
(5 answers)
Closed 3 years ago.
I have some SQL code that I want to "convert" to an entity framework 'call'.
This is the code:
select itemStatus, avg(UnitPrice) from ItemSalesHistory
where item = 'BQF09-L-Q007'
group by itemStatus
I'm new to using Entity Framework.
Yes you can. If you take a look at this answer, you can apply it to your situation:
var query = ItemSalesHistory
.Where(x => x.item == 'BQF09-L-Q007')
.GroupBy(x => x.itemStatus)
.Select(g => new { itemStatus = g.Key, avg = g.Average(x => x.UnitPrice) });

How to combine a query of the LINQ and a list? [duplicate]

This question already has answers here:
Linq version of SQL "IN" statement
(6 answers)
Closed 4 years ago.
[Sorry if it is a duplicate]
I couldn't find properly solution, so I decided to ask a question.
I have an object companies which returns list of elements.
I would like to write a query which will select these all CompanyId which we have in our list. I don't want to select only one record by using FirstOrDefault().
Example:
var companiesSummary = _context.Company
.Where(c => c.CompanyId == companies.Select(cs => cs.ID))
.Include(country => country.Country)
How can I cope with it? Do you have any ideas?
Select the ids of the companies from your in-memory list and then pass that into the query in the where method:
var ids = companies.Select(cs => cs.ID).ToList();
var companiesSummary =
_context.Company
.Where(c => ids.contains(c.ID))
.Include(country => country.Country)
Assuming your companies contains a list of objects with an ID property you want to compare to Company.CompanyId, your query should look like
int[] ids = companies.Select(cs => cs.ID).ToArray();
var companiesSummary = _context.Company
.Where(c => ids.Contains(c.CompanyId))
.Include(company => company.Country);
var matchingCompanies = companies.Where(c => companyIds.Contains(c.Id))
Make companyIds a HashSet<T> for an efficient Contains.

C# Is it possible to check if any specific digit is present in an ID? [duplicate]

This question already has answers here:
Integer Contains Using Linq
(2 answers)
Closed 6 years ago.
I want to have a search option where
1.user will type any letter that matches the name from database table.
This works smoothly.
string userinput = "hel"
List<string> queryResult = _myContext.Products
.Where(r => r.Name.Contains(userinput))
.Select(r => r.Name)
.ToList();
2. user will type any digit that should match the ID if that digit/digits exist in the ID it should give me a list of those ID
can i do something thing like that??
int queryId= 12;
_myContext.Products.Where(r => r.ID.contains(queryId)).Select(r => r.ID).ToList();
string queryId = "12";
List<string> queryResult = _myContext.Products
.Where(r => r.Id.Tostring().Contains(queryId))
.Select(r => r.ID)
.ToList();

Where clause against multiple options [duplicate]

This question already has answers here:
What is the linq equivalent to the SQL IN operator
(9 answers)
Closed 8 years ago.
Is there any "IN" keyword available in Linq to entities syntax ? I have a listbox where multiple items are added. I want to search for those items in db. My query looks like this:
var result = context.data_vault
.Where(d => d.STATE == lstStates.SelectedItem.Text).OrderBy(d=>d.dv_id)
.Skip(e.NewPageIndex * GridView1.PageSize)
.Take(GridView1.PageSize)
.ToList();
I want d => d.STATE == lstStates.Items. How to change it?
The code:
string[] arr = listview.Items
.Cast<ListItem>()
.Select(i => i.Text) // or .Value
.ToArray();
db.Where(d => arr.Contains(d.STATE);
should be translated into the query:
... WHERE STATE IN ('your', 'array', 'items')
Try this:
var result = context.data_vault
.Where(d => lstStates.Items.Contains(d.STATE)).OrderBy(d=>d.dv_id)
.Skip(e.NewPageIndex * GridView1.PageSize)
.Take(GridView1.PageSize)
.ToList();
Here,
lstStates is List<string>
You can try .Any() or .Contains() syntax
example something like this:
select d from context.Data
Where ListofIdsFromUI.Any(d.Id)
select d

Categories