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.
Related
I am trying to get the same results as with a SQL query using Entity Framework method syntax.
SQL query :
select
mr.*, mrf.userId as RequesterUserId, mrt.UserId as ReceiverUserId
from
MoneyRequests mr
inner join
MoneyReqFrom mrf on mr.MoneyRequestId = mrf.MoneyRequestId
inner join
MoneyReqTo mrt on mr.MoneyRequestId = mrt.MoneyRequestId
where
mr.MoneyRequestId = 'acfc8008-4cf7-47ec-a3fe-0fe245af77cc'
EF Linq method syntax :
var moneyreqResponse = context.MoneyRequests
.Join(context.MoneyReqFroms,
mr => mr.MoneyRequestId,
mrf => mrf.MoneyRequestId,
(mr, mrf) => new
{
MoneyRequestId = mr.MoneyRequestId,
Amount = mr.Amount,
RequestType = mr.RequestType,
CreationDate = mr.CreationDate,
RequesterUserId = mrf.UserId
})
.Join(context.MoneyReqTos,
mr => mr.MoneyRequestId,
mrt => mrt.MoneyRequestId,
(mr, mrt) => new
{
MoneyRequestId = mr.MoneyRequestId,
Amount = mr.Amount,
RequestType = mr.RequestType,
CreationDate = mr.CreationDate,
ReceiverUserId = mrt.UserId,
Email = mrt.Email
})
.Where(fullEntry => fullEntry.MoneyRequestId == "acfc8008-4cf7-47ec-a3fe-0fe245af77cc")
.ToList();
I retrieve the data from the database except the column RequesterUserId.
Do you know why?
Thanks
Your query returns a MoneyRequests type, which I belive does not contain RequesterUserId.
You should define a new type with all the properties you need (those returned by the join) and add it to your DbContext.
Also probably you want to mark your new type as keyless
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().
I'm trying to get the union of these two queries but keep getting the following error:
'System.Linq.IQueryable<AnonymousType#1>' does not contain a definition for 'Union' and the best extension method overload 'System.Linq.ParallelEnumerable.Union<TSource>(System.Linq.ParallelQuery<TSource>, System.Collections.Generic.IEnumerable<TSource>)' has some invalid arguments
The linq queries look like this:
var g = from p in context.APP_PROD_COMP_tbl
where p.FAM_MFG == fam_mfg
group p by new
{
a_B_G = p.B_G,
a_MFG = p.MFG,
a_PRODUCT_FAM = p.PRODUCT_FAM,
};
var q = from p in context.APP_COMP_tbl
where p.FAM_MFG == fam_mfg
group p by new
{
a_B_G = p.a_B_G,
a_MFG = p.a_MFG,
a_PRODUCT_FAM = p.a_PRODUCT_FAM,
};
var data = q.Union(g);
I've tried using IEnumerable around the queries, but it still didn't work. Not really sure where I'm going wrong at this point, although admittedly LINQ isn't something I've had a ton of exposure to.
Update:
So I've gone in a slightly different direction from what I posted earlier. After doing more research, the group by statements were from old code and no longer needed for the intended purpose. I changed those to select new statements and had no further issue with the union.
I think that your problem here is type mismatch: g is of type IGrouping<AnonymousType#1, APP_PROD_COMP_tbl> and q is of type IGrouping<AnonymousType#1, APP_COMP_tbl>; this is why Union gives you the error.
I am not really sure what you are trying to Union (keys of the groups or groups of data themselves) but the solution would be:
If you want to union group keys, select the keys of your groups
var data = g.Select(x => x.Key).Union(q.Select(x => x.Key));
If you want to union the groups themselves then you need to project each element from both sequences into a common type, perform the grouping and then union the groups
var g = context.APP_PROD_COMP_tbl
.Where(p => p.FAM_MFG == fam_mfg)
.Select(ToCommonType)
.GroupBy(p => new
{
a_B_G = p.B_G,
a_MFG = p.MFG,
a_PRODUCT_FAM = p.PRODUCT_FAM,
});
var q = context.APP_COMP_tbl
.Where(p => p.FAM_MFG == fam_mfg)
.Select(ToCommonType)
.GroupBy(p => new
{
a_B_G = p.a_B_G,
a_MFG = p.a_MFG,
a_PRODUCT_FAM = p.a_PRODUCT_FAM,
});
var data = g.Union(q);
private CommonClass ToCommonType(APP_PROD_COMP_tbl item)
{
return new CommonClass
{
};
}
private CommonClass ToCommonType(APP_COMP_tbl item)
{
return new CommonClass
{
};
}
The problem is your Anonymouse types don't match:
var a = Enumerable.Range(1, 10).Select(x => new {a = x}).AsQueryable();
var b = Enumerable.Range(1, 10).Select(x => new {b = x}).AsQueryable();
var c = a.Union(b);
This won't work because typeof a is not same as typeof b
var a = Enumerable.Range(1, 10).Select(x => new {a = x}).AsQueryable();
var b = Enumerable.Range(1, 10).Select(x => new {a = x}).AsQueryable();
var c = a.Union(b);
But this will work, because Anonymouse types are the same.
You can try selecting same anonymouse types from your collection in q and g. Read more about Union for IQueryable
Union on IQueryAble<TSource>() accepts IQueryAble<TSource> as a parameter, so collection has to be the same type.
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);
}
}
Normally, I do this:
var a = from p in db.Products
where p.ProductType == "Tee Shirt"
group p by p.ProductColor into g
select new Category {
PropertyType = g.Key,
Count = g.Count() }
But I have code like this:
var a = Products
.Where("ProductType == #0", "Tee Shirt")
.GroupBy("ProductColor", "it")
.Select("new ( Key, it.Count() as int )");
What syntax could I alter to produce identical results, i.e., how do I do a projection of Category from the second Linq statement?
I know in both that g and it are the same and represent the entire table record, and that I am pulling the entire record in just to do a count. I need to fix that too. Edit: Marcelo Cantos pointed out that Linq is smart enough to not pull unnecessary data. Thanks!
Why would you have to do it at all? Since you still have all of the information after the GroupBy call, you can easily do this:
var a = Products
.Where("ProductType == #0", "Tee Shirt")
.GroupBy("ProductColor", "it")
.Select(c => new Category {
PropertyType = g.Key, Count = g.Count()
});
The type of Products should still flow through and be accessible and the regular groupings/filtering shouldn't mutate the type that is flowing through the extension methods.