How can I make a LINQ query that would return a new list of distinct Operators from the Workpacks table (DB is not normalized). I got this query, but I don't know how to use DISTINCT properly, so that SQL server doesn't return duplicate values
Instance.CriteriaOperatorList =
(from v in context.Workpacks
select new FilterCriteriaItem()
{
GUID = Guid.NewGuid(),
Name = v.Operator,
}).ToList();
You want to get the distinct Operators from the Workpack, then get those. The easiest way to do that is to not use the SQL LINQ syntax.
Instance.CriteriaOperatorList = context.Workpacks
// Only request the Operator names
.Select(w => w.Operator)
// But just request the distinct names
.Distinct()
// Then select into your DTO.
.Select(o => new FilterCriteriaItem
{
GUID = Guid.NewGuid(),
Name = o // o is the Operator from Workpack
})
.ToList();
Just use .Distinct() before .To list().
Related
I'm using MongoDB C# driver 2.4.4 in my web application. I need to group documents in a collection filtering them dynamically.
var query = collection.Aggregate()
.Match(y => y.IdLower.Contains(id))
.Match(y => y.NameLower.Contains(name))
.Group(
key => key.Id,
g => new
{
Id = g.Key
}).ToList();
I need to add or remove Match operators based to user input but I cannot figure how.
I tried something like this:
var query = collection.Aggregate();
if(!string.IsNullOrWhiteSpace(id))
query = query.Match(y => y.IdLower.Contains(id));
if (!string.IsNullOrWhiteSpace(name))
query = query.Match(y => y.NameLower.Contains(name));
query = query.Group(
key => key.Id,
g => new
{
Id = g.Key
}).ToList();
but I get syntax error Cannot imlicitly convert type System.Collection.Generic.List<<anonymous type: string Id>> to '...
How to achieve something like this?
The problem is that the type of query is IAggregateFluent<T1> (where T1 is your document type), but the return type of the .Group() method is IAggregateFluent<T2> (where T2 is an anonymous type). The compiler does not know how to implicitly convert these types, hence the error.
It depends on what you're trying to do here, but one possible way to fix this would be to return instances of T1 (your document type) from the group expression:
query = query.Group(
key => key.Id,
g => new T1 // replace "T1" with the actual name of your class
{
Id = g.Key
}).ToList();
Another option would be to assign the results of the group function to a new variable:
var grouped = query.Group(
key => key.Id,
g => new
{
Id = g.Key
}).ToList();
Hope this helps.
I am attempting to order the results of a Linq query by the length of a property and then by the property itself in order to order a string as an integer but the generated SQL is not ordering as I would expect it to.
I am joining multiple tables, filtering it down, selecting a DTO out with:
query = basequery.Select(s => new HeadersDTO
{
headerid = s.Header.id,
orderno = s.Header.orderno,
customer = s.Header.customer,
dateoforder = s.Header.dateoforder,
consignee = s.Location.name,
city = s.Location.name,
state = s.Location.state
}).Distinct();
Then trying to order by s.Header.orderno
query = query.OrderByDescending(x => x.orderno.Length).ThenByDescending(x => x.orderno)
.Skip(() => offset).Take(() => criteria.per_page);
This still orders it the normal way strings are ordered with first character taking precedence.
But if I select the x.orderno.Length out into it's own property and then order by that it works e.g.
query = basequery.Select(s => new HeadersDTO
{
ordernolength = s.Header.orderno.Length <---- added this
headerid = s.Header.id,
orderno = s.Header.orderno,
customer = s.Header.customer,
dateoforder = s.Header.dateoforder,
consignee = s.Location.name,
city = s.Location.name,
state = s.Location.state
}).Distinct();
query = query.OrderByDescending(x => x.ordernolength).ThenByDescending(x => x.orderno)
.Skip(() => offset).Take(() => criteria.per_page);
Is there a way to do this where I don't have to create a new property in the select list? I can add more information if needed.
Try to create a custom Comparer using IComparer where you do the Int32 check for this field. Here is an example for this:
Use own IComparer<T> with Linq OrderBy
Hope this helps
List<Guid?> MobileAppID = new List<Guid?>();
return d
.Where(x => x.MarketplaceID IN MobileAppID)
.ToList();
I want to select values from the d where MarketplaceID in MobileAppID.MobileAppID is a set
How would i do that in LINQ C#. Something like select in query in SQL Server
d is a class containing MarketplaceID
The equivalent would be a Contains query:
return d
.Where(x => MobileAppID.Contains(x.MarketplaceID))
.ToList();
Might want to make MobileAppID a HashSet to speed this up.
How can I do this SQL query with Entity Framework?
SELECT DISTINCT NAME FROM TestAddresses
Using lambda expression..
var result = EFContext.TestAddresses.Select(m => m.Name).Distinct();
Another variation using where,
var result = EFContext.TestAddresses
.Where(a => a.age > 10)//if you have any condition
.Select(m => m.name).Distinct();
Another variation using sql like syntax
var result = (from recordset
in EFContext.TestAddresses
.where(a => a.city = 'NY')//if you have any condition
.select new
{
recordset.name
}).Distinct();
Try this:
var results = (from ta in context.TestAddresses
select ta.Name).Distinct();
This will give you an IEnumerable<string> - you can call .ToList() on it to get a List<string>.
The way that #alliswell showed is completely valid, and there's another way! :)
var result = EFContext.TestAddresses
.GroupBy(ta => ta.Name)
.Select(ta => ta.Key);
I hope it'll be useful to someone.
DBContext.TestAddresses.Select(m => m.NAME).Distinct();
if you have multiple column do like this:
DBContext.TestAddresses.Select(m => new {m.NAME, m.ID}).Distinct();
In this example no duplicate CategoryId and no CategoryName i hope this will help you
Entity-Framework Select Distinct Name:
Suppose if you are using Views in which you are using multiple tables and you want to apply distinct in that case first you have to store value in variable & then you can apply Distinct on that variable like this one....
public List<Item_Img_Sal_VIEW> GetItemDescription(int ItemNo)
{
var Result= db.Item_Img_Sal_VIEW.Where(p => p.ItemID == ItemNo).ToList();
return Result.Distinct().ToList();
}
Or you can try this Simple Example
Public Function GetUniqueLocation() As List(Of Integer)
Return db.LoginUsers.Select(Function(p) p.LocID).Distinct().ToList()
End Function
use Select().Distinct()
for example
DBContext db = new DBContext();
var data= db.User_Food_UserIntakeFood .Select( ).Distinct();
In order to avoid ORDER BY items must appear in the select list if SELECT DISTINCT error, the best should be
var results = (
from ta in DBContext.TestAddresses
select ta.Name
)
.Distinct()
.OrderBy( x => 1);
Entity-Framework Select Distinct Name:
Suppose if you are want every first data of particular column of each group ;
var data = objDb.TableName.GroupBy(dt => dt.ColumnName).Select(dt => new { dt.Key }).ToList();
foreach (var item in data)
{
var data2= objDb.TableName.Where(dt=>dt.ColumnName==item.Key).Select(dt=>new {dt.SelectYourColumn}).Distinct().FirstOrDefault();
//Eg.
{
ListBox1.Items.Add(data2.ColumnName);
}
}
I am trying to accomplish something like this query:
var query = from a in DatabaseTable
where listOfObjects.Any(x => x.Id == a.Id)
select a;
Basically, I want to filter the results where a.Id equals a property of one of the objects in the generic list "listOfObjects". I'm getting the error "Local sequence cannot be used in LINQ to SQL implementation of query operators except the Contains() operator."
Any ideas on how to filter this in an easily readable way using "contains" or another method?
Thanks in advance.
Just project your local list into a list of the specific items you need to filter on:
var listOfIds = listOfObjects.Select(o => o.Id);
var query =
from a in DatabaseTable
where listOfIds.Contains(a.Id)
select a;
var listOfIds = listOfObjects.Select(x => x.Id).ToList();
var query = from a in DatabaseTable
where listOfIds.Contains(a.Id)
select a;