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 = ...
Related
...I've tried something but I got the "Local sequence cannot be used in LINQ to SQL implementations of query operators except the Contains operator." exception.
I'm talking about this:
Query = Query.Where(t => this.SysTextBox.Text.CSL2Array().All(ss => t.SysName.Contains(ss)));
I'm kind of new at this, but I kept trying to make it work. Thank you in advance!
You should be able to do the query after ToList:
Query = Query.ToList<TagsDeleted>()
.Where(t => this.SubSystemTextBox.Text.CSL2Array().All(ss => t.SubsystemName.Contains(ss)))
.AsQueryable<TagsDeleted>();
But that is not easy to read, I would refactor to the following to make it clear that "I create a list, and want to remove some items":
var words = this.SubSystemTextBox.Text.CSL2Array();
var list = Query.ToList<TagsDeleted>();
list.RemoveAll(t => !words.All(word => t.SubsystemName.Contains(word)));
Query = list.AsQueryable<TagsDeleted>();
var arr = this.SubSystemTextBox.Text.CSL2Array();
var notContained = Query.Where(td=>arr.Any(ss => !td.SubsystemName.Contains(ss)));
Query = Query.Except(notContained);
You can figure this out how to make it in one line, this was just to be clear.
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.
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())
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();