Hi I am trying to OrderByDescending() on a query but rather than:
results = results.OrderByDescending(o => o.Surname);
I wish to use:
results = results.OrderByDescending(o => "Surname");
The value in quotes would be passed in in a parameter.
I was looking at reflection but am unsure.
Have a look at LINQ Dynamic Query Library: http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx
this will allow you to use results.OrderByDescending("Surname");
I use something like this
results = results.OrderByDescending(o => o.GetType().GetProperty("Surname").GetValue(o, null).ToString())
Related
I want do something like this:
string test = alarmType;
db.Alarms.Where(alarmType.Contains(m => m.Type)).ToList();
But this doesn't work. How can I make such query? Is it the only way to use pure SQL?
UPD
I'm trying to find whether records is substring of the "test", not conversly.
You have to reverse the condition:
var query = db.Alarms
.Where(a => alarmType.Contains(a.Type))
.ToList();
However, your code sample is confusing, if alarmType is a string i don't know what you're trying to achieve.
string test = alarmType;
Update: if you're using LINQ-To-Sql and you want to find all records where the Type is a substring of alarmType you can use:
var query = db.Alarms
.Where(a => SqlMethods.Like(alarmType, string.Format("%{0}%", a.Type)))
.ToList();
Try the following
string test = alarmType;
var result = db.Alarms.Where(m => alarmType.Contains(m.Type)).ToList();
Your LINQ query isn't well-formatted. You have:
db.Alarms.Where(alarmType.Contains(m => m.Type)).ToList();
So the parameter you've passed to Contains is a lambda, which isn't what Contains takes,
Likeiwse, Contains returns a bool, so you've passed a bool to Where, which is also not a parameter type it takes.
What you want is to pass a lambda to Where, like so:
db.Alarms.Where(m => alarmType.Contains(m.Type)).ToList();
Note how now both the Where and the Contains are being passed parameters of the correct type.
How would I write this query in dynamic linq? I know how to do the select, but how do I do the "let"?
var qry = from sr in data.Addresses
let AddressType_Desc = sr.AddressType.AddressType_Desc
let Country_Desc = sr.Country.Country_Desc
where sr.Customer_GUID == CustomerGuid
select new
{
sr.Address_GUID,
sr.People_GUID,
sr.AddressType_GUID,
AddressType_Desc,
sr.Address1,
sr.Address2,
sr.City,
sr.State,
sr.Zip,
sr.County,
sr.Country_GUID,
Country_Desc,
sr.UseAsMailing
};
There is no equivalent of let in linq expression method syntax, as well in dynamic LINQ.
Let can only help you to make your queries more readable. It simply works as an alias or local variable. You can imagine, that in method syntax you won't be able to access it outside the scope of the method declared it.
In your case, just simply put the let variable initiation into the select.
Like this in linq method syntax:
var qry = data.Adresses.Where(sr => sr.Customer_GUID == CustomerGuid)
.Select(sr => new {
sr.Address_GUID,
....
sr.AddressType.AddressType_Desc,
sr.Country.Country_Desc
});
Or similar with dynamic LINQ (select clause as string):
var qry = data.Adresses.Where(sr => sr.Customer_GUID == CustomerGuid)
.Select("new (Address_GUID, AddressType.AddressType_Desc, Country.Country_Desc)");
And you will get the same result as with linq query syntax.
It would be similar for other expression methods. Only thing you need, is to use the value directly instead of the let alias.
I have a linq query with NHibernate using Session.Query<T> method and I in this query I Fetch some complex properties and collection properties. I would like to know, how can I add an condition with IN operator from an int[]? Look my code:
public IEnumerable<Product> GetProducts(int[] idCategories)
{
// how to add IN condition here or a subquery
var query = Session.Query<Product>()
.Where(?????)
.Fetch(x=>x.Category)
.FetchMany(x=>x.Status).ThenFetch(x=>x.Item);
return query.ToList();
}
I have another method doing a query to get this int[] and I would like to apply it here, or if is there any way to add this subquery on the IN operator, I really appreciate!
OBS: I could convert int[] to List<int> if its necessary.
Edits
I got this int[] by a query like:
return session.Query<Category>.Where(...).Select(x => x.Id).ToArray();
My second question is, how could I add this query as a subquery to filter by category?
Thank you!
You don't really need the IN operator. You can just do it like this:
.Where(x => idCategories.Contains(x.Category))
Note: Contains is an extension method. You need to ensure that you have a using statement for System.Linq, but you probably already have it.
Take a look on Restrictions, for example
var query = Session.Query<Product>()
.Where(Restrictions.In(Projections.Property<Product>x=>x.Id),idCategories))
.Fetch(x=>x.Category)
.FetchMany(x=>x.Status).ThenFetch(x=>x.Item);
About sub query, you need Subqueries class
var query = Session.Query<Product>()
.Where(Subqueries.WhereProperty<Product>(x=>x.Id)...)
.Fetch(x=>x.Category)
.FetchMany(x=>x.Status).ThenFetch(x=>x.Item);
for more details please look here How to do subqueries in nhibernate?
How is the OrderByDescending used ?
I have a label, Circles, declared like this
ReadOnlyCollection<FlangeCircle> Circles
which contain a variabel, Diameter of the type double
I want to sort them based on the diamter so I try
FlangeCircle<FlangeCircle> query = Circles.OrderByDescending(p => p.Diameter);
but that will not go throug the compiler, but the following does
var query = Circles.OrderByDescending(p => p.Diameter);
Why is that and how do I declare query with a "correct" type instead ?
/Stefan
The type of the variable is the problem:
FlangeCircle<FlangeCircle> query = ...
FlangeCircle<FlangeCircle> doesn't make sense as a type, and certainly isn't what's returned by OrderByDescending. You almost certainly want:
IEnumerable<FlangeCircle> query = Circles.OrderByDescending(p => p.Diameter);
Or if you want to be able to perform ThenBy/ThenByDescending operatorions on query:
IOrderedEnumerable<FlangeCircle> query = ...;
This will return an IEnumerable, so:
IEnumerable<FlangeCircle> query = Circles.OrderByDescending(p => p.Diameter);
Shouldn't the variable by IEnumerable? as in
IEnumerable<FlangeCircle> query = Circles.OrderByDescending(p => p.Diameter);
Try to use IEnumerable<FlangeCircle> query = ...
I just want to know what's the lambda expression of Select * from TableName.
Like in plain LINQ it will be var res=from s in db.StudentDatas select s;
here StudentData is name of the table.
Thanks.
The lambda expression isn't needed:
var res = db.StudentDatas;
You could use one but it would be rather pointless:
var res = db.StudentDatas.Select(s => s);
The compiler will translate it to something along these lines:
db.StudentDatas.Select(s => s)
The translation to SQL is done by the Base Class Library. SQL, of course, does not use lambda expressions...
You don't require a lambda expression. You just want all members of the collection.
While both are technically correct, as in they would both give the same result in its simplistic form, this is only because the default behavior of db.table is “Select”. Behind the scenes, they are different. While one is a System.Data.Linq.Table, the other is System.Linq.IQuerable.
For example
var x = db.table ; var y= db.table(s=>s);
X = y;
would result in a compiler error.
When using the Dynamic Linq library, in cases where you have to create dynamic queries at runtime, you have to use an IQuerable as your initial select. Which means var qry = db.table(s=>s); as opposed to var qry = db.table;
Then you can go on and chain your queries like: qry = qry.Where(bla bla bla);
Found it out the hard way after a few nail biting sessions.
Code:
var allmember = eg_.table.where(x=>x).ToList();