My Database Table structure:
Schema Property Type
For different 'Schema' values I have same 'Property' and 'Type' values.
My linq query:
IEnumerable<CModel> model = db.dbModels.AsEnumerable().Select(o => new CModel { CName = "XXX", PName = o.Property, PType = o.Type }).Distinct().ToList();
What I'm trying to do is get distinct 'Property' and 'Type' values but the query is giving me duplicate values.
Thanks
You can group them:
IEnumerable<CModel> model = db.dbModels.AsEnumerable()
.GroupBy(x => new { x.Property, x.Type })
.Select(x => new CModel
{ CName = "XXX",
PName = x.Property,
PType = x.Type }).ToList();
Related
I have a requirement to get data based on combination of properties with OR and AND combination and i am not able to get this query done. Help me with what it should be like in json and also using NEST client.
SQL query:
Select *
From MySourceTable
Where (flag1 In ("Y","N")
And EntityStatus In ("STAT1", "STAT2")
And EntityType In ("T3"))
Or (flag1 In ("N") And EntityType In ("T1", "T2"))
So far my query builds all must queries which resulted in fewer or none results. So theoretically I believe each query is MUST queries and all must queries have to be merged using OR.
Help me build json
You are on a good way, the query will look something like
var searchResponse = await elasticClient.SearchAsync<EsDocument>(s => s
.Query(q =>
(
q.Terms(t => t.Field(f => f.Flag1.Suffix("keyword")).Terms("Y", "N")) &&
q.Terms(t => t.Field(f => f.EntityStatus.Suffix("keyword")).Terms("STAT1", "STAT2")) &&
q.Terms(t => t.Field(f => f.EntityType.Suffix("keyword")).Terms("T3"))
) ||
(
q.Terms(t => t.Field(f => f.Flag1.Suffix("keyword")).Terms("N")) &&
q.Terms(t => t.Field(f => f.EntityType.Suffix("keyword")).Terms("T1", "T2"))
)));
You didn't share your index mapping and query, so hard to say why your query didn't work.
UPDATE
You can compose such query dynamically using the following
var should = new QueryContainer();
should |=
(
new TermsQuery { Field = Infer.Field<EsDocument>(f => f.Flag1.Suffix("keyword")), Terms = new []{ "Y", "N" }} &&
new TermsQuery { Field = Infer.Field<EsDocument>(f => f.EntityStatus.Suffix("keyword")), Terms = new []{ "STAT1", "STAT2" }} &&
new TermsQuery { Field = Infer.Field<EsDocument>(f => f.EntityType.Suffix("keyword")), Terms = new []{ "T3" }}
);
should |=
(
new TermsQuery { Field = Infer.Field<EsDocument>(f => f.Flag1.Suffix("keyword")), Terms = new []{ "N" }} &&
new TermsQuery { Field = Infer.Field<EsDocument>(f => f.EntityType.Suffix("keyword")), Terms = new []{ "T1", "T2" }}
);
var searchResponse = await elasticClient.SearchAsync<EsDocument>(s => s
.Query(q => should));
List<branch_rating> ObjRat = new List<branch_rating>();
ObjRat=(_Context.branch_rating
.GroupBy(a => a.restaurant_branch_id)
.Select(a => new { rating = a.Sum(b => b.rating), Name = a.Key })
.OrderByDescending(a => a.rating)).ToList();
I tried by putting query in var variable and then adding in object.
Below picture shows the error.
I want to get data from branch_rating table having four columns, I also tried by using var and then adding list data one by one in object.
It will be better if you make a custom class like DOTopResturant which includes data members branch_rating,branch_id and then make a object of type DOTopResturant , add every item in variable q into that object.
List<branch_rating> ObjRat = new List<branch_rating>();
ObjRat = _Context.branch_rating.ToList();
var q = (_Context.branch_rating
.GroupBy(a => a.restaurant_branch_id)
.Select(a => new { branch_rating = a.Sum(b => b.rating), branch_id = a.Key })
.OrderByDescending(a => a.branch_rating)).ToList();
List<DOTopResturant> ObjTopRes = new List<DOTopResturant>();
foreach (var item in q)
{
ObjTopRes.Add(new DOTopResturant() { branch_id = Convert.ToInt32(item.branch_id), branch_rating = Convert.ToInt32(item.branch_rating) });
}
return ObjTopRes;
Just read and understand what the error describes :
your are trying to convert an anonymous type (with 2 properties Name and Rating) to another object type branch_rating.
You have 2 solutions :
work with the anonymous type (using var) :
var ObjRat=(_Context.branch_rating
.GroupBy(a => a.restaurant_branch_id)
.Select(a => new { Rating = a.Sum(b => b.rating), Name = a.Key })
.OrderByDescending(a => a.rating))
.ToList();
And then, in your code, just access to Rating and Name properties of your list members.
work with your type defined branch_rating, by using it in your Select, and use the appropriate constructor.
List<branch_rating> ObjRat = new List<branch_rating>();
ObjRat=(_Context.branch_rating
.GroupBy(a => a.restaurant_branch_id)
.Select(a => new branch_rating() { <Your branch_rating properties assignments here> })
.OrderByDescending(a => a.rating)).ToList();
If your branch_rating object does not have an empty constructor, call the correct constructor with good arguments in the Select.
Change select (third row in the picture) to:
.Select(a => new DOTopRestaurant {rating = a.Sum(b => b.rating), Name = a.Key})
Delete the declaration of ObjRat and use var:
var ObjRat=(_Context.branch_rating
.GroupBy(a => a.restaurant_branch_id)
.Select(a => new { rating = a.Sum(b => b.rating), Name = a.Key })
.OrderByDescending(a => a.rating))
.ToList();
Your list is a list of the anonymous type not of type branch_rating.
If you want to convert it to branch_rating use another Select to create a new branch_rating(...) passing the appropriate parameters to the constructor.
PS You should also look at .NET naming conventions.
I use the following LINQ to create grid model:
...
var query = from a in GetUser()
select new UserModel
{
ID = a.ID,
StartDate = a.StartDate,
EndDate = a.EndDate,
StateName = a.State.StateName,
StateID = a.StateID,
City = a.City,
};
return query;
....
and the HTML is
#Html.Grid(Model.PagedList).Columns(
col =>
{
col.For(c => c.StateName).Named("State").Attributes(#class => "row").HeaderAttributes(#class => "head");
col.For(c => c.City).Named("City").Attributes(#class => "row").HeaderAttributes(#class => "head");
col.For(c => c.StartDate).Named("Start Date").Attributes(#class => "row").HeaderAttributes(#class => "head");
col.For(c => c.EndDate).Named("End Date").Attributes(#class => "row").HeaderAttributes(#class => "head");
col.For(c => Html.ActionLink("Details", "Details", new { id = c.ID })).Named("View Details").HeaderAttributes(#class => "head").DoNotEncode();
}).Sort(Model.GridSortOptions).Attributes(#class => "grid")
The main problem is how to show only DATE without TIME part?
I tried some options but in some cases I got the errors and in other cases sorting AZ doesn't work at all.
Any clue?
Thank you!!!
Try using
col
.For(c => c.EndDate.ToLongDateString())
.Named("End Date")
.Attributes(#class => "row")
.HeaderAttributes(#class => "head");
Something a little easier I find is the Format method on the column. see docs here
So your code looks something like this:
col.For(c => c.EndDate).Format("{0:d"})
//use .Format("{0:yyyy-MM-dd}") to get 2014-10-21
If you want to use ToShortDateString then you will need to define the column name as well as the sort column name, for example:
col.For(c => c.EndDate.ToShortDateString())
.Named("End Date")
.SortColumnName("EndDate");
I want to get the list of sales for each month ordered by product.
Currently I have the following linq query:
var query = storeDB.OrderDetails
.Select(od => od)
.GroupBy(r => r.Product)
.Select(group => new ProductSalesModel {
Product = group.Key,
Sales = group.Sum(s => s.Quantity),
Amount = group.Sum(s => s.Quantity * s.UnitPrice)
})
.OrderByDescending(x => x.Amount);
How should I do to also group the list by month getting the sales date from my Order table ? Order.CreationDate?
I don't know what Linq provider you are trying to use but in plain Linq this should work:
.GroupBy(r => new {r.Product, r.CreationDate})
and then when you select:
.Select(group => new ProductSalesModel {
Product = group.Key.Product,
CreationDate = group.Key.CreationDate,
Sales = group.Sum(s => s.Quantity),
Amount = group.Sum(s => s.Quantity * s.UnitPrice)
})
If you want to order by something it needs to be in the grouping.
Then you can use OrderByDescending(t=> t.amount).ThenBy(y => y.OrderDate)
how can i show null or empty fields in last by apply Orderby:
here is my code.
Var movies = _db.Movies.Orderby(c => c.Category).ThenBy(n => n.Name)
If I understand your question correctly:
Var movies = _db.Movies
.OrderBy(c => c.Category==null?1:0)
.Thenby(c => c.Category).ThenBy(n => n.Name)