linq query with list - linq

i have a simple query. Method-1 works well. But i don't understand what is wrong with method-2?
//method-1
List<string> li = new List<string>();
List<string> liSNB = new List<string>();
li.Add("result1");
li.Add("result2");
li.Add("result3");
var vQuery = from ssoli in li.AsEnumerable()
where li.Contains(ssoli)
where ssoli.Contains("2")
select new
{
soName = ssoli,
};
liSNB.Clear();
foreach (var v in vQuery)
liSNB.Add(v.soName);
li.Clear();
li.AddRange(liSNB);
lbxLinq.Items.AddRange(li.ToArray());//add results2
//method-2
List<string> liSNB = new List<string>();
liSNB.Add("result1");
liSNB.Add("result2");
liSNB.Add("result3");
var vQuery = from ssoli in liSNB.AsEnumerable()
where liSNB.Contains(ssoli)
where ssoli.Contains("2")
select new
{
soName = ssoli,
};
liSNB.Clear();
foreach (var v in vQuery)
liSNB.Add(v.soName);
lbxLinq.Items.AddRange(liSNB.ToArray());//add nothing WHY???
Why no results in method-2 why? I have been workind for 2 days on just this situation. Is it bug or something? Thank you for answers initially.

Since the linq uses deferred execution,your query is actualy executing in here:
foreach (var v in vQuery)
In your second code snippet you are removing all the items from liSNB before executing the query.So it doesn't return anything.
Btw this doesn't make any little sense, you can remove it:
where liSNB.Contains(ssoli)
You can read more about deferred execution in here and here

I'll run over some mistakes in your second code block; most of them apply to the first block too.
List<string> liSNB = new List<string>();
liSNB.Add("result1");
liSNB.Add("result2");
liSNB.Add("result3");
The AsEnumerable in this line achieves nothing:
var vQuery = from ssoli in liSNB.AsEnumerable()
This Contains check achieves nothing - of course ssoli is in the list or it wouldn't show up in the query:
where liSNB.Contains(ssoli)
This is fine:
where ssoli.Contains("2")
There's no reason to nest the string you want here - just select ssoli would work:
select new
{
soName = ssoli,
};
The query has not run yet - it is deferred. By clearing the list here, you have nothing to query over:
liSNB.Clear();
While you are in the foreach loop the query is "running". By altering the source list while the query is running, you will cause the query to throw an exception. (You'd also need to remove soName. here if you stopped selecting it above):
foreach (var v in vQuery)
liSNB.Add(v.soName);
(Actually in your example you only have one matching item so you get away with it. If you had two or more items matching the query you'd be in trouble).
The ToArray is pointless here: I stand corrected, the method takes an object[].
lbxLinq.Items.AddRange(liSNB.ToArray());

Linq query executes when the result is accessed, so you need to call ToArray() or ToList() after query:
var vQuery = (from ssoli in liSNB.AsEnumerable()
where liSNB.Contains(ssoli) // this line is pointless because you are checking in same list
where ssoli.Contains("2")
select new
{
soName = ssoli,
}).ToList();
Now your query will get executed when ToList() is called and result will be in vQuery.
The line where liSNB.Contains(ssoli) is not needed because you checking the source list item in the source list, so all items will be filtered, means no filtering will apply.

Related

How do I know if my linq will return a useable object or a null

I am working on a web service. I am using linq to query a database. Seemingly simple, but I've run into an issue. Here is my code for reference:
List<Comment> res = new List<Comment>();
using (ApplicationHistoryEntities ahe = new ApplicationHistoryEntities())
{
res = (from columns in ahe.Comments
where columns.NetforumId == actionuniqueid
select columns).ToList();
}
If I have no entries in the database, will my .ToList() throw an error? I could deploy it, and just try it out but I want to know more about the mechanism that my linq is using. If ahe.Comments database has no rows... what will the (from...) section return?
I could just add a null reference check, use dynamics etc but I want to really understand it.
I found this Q: how to know if my linq query returns null but it seems like all of the answers are in conflict on how it really should be done...
example answers:
Either you can convert it to list and then check the count
Best approach is to check there is null(no items) in list use Any() instead of count()
LINQ queries should never return null and you should not get an exception if the result is empty. You probably have an error in your code.
You can realise the result as a list then check the items.
You can see why I question how it works.
Edit:
Final code looks like this:
List<Comment> res;
using (ApplicationHistoryEntities ahe = new ApplicationHistoryEntities())
{
res = ahe.Comments?.Where(rowItem => rowItem.NetforumId == actionuniqueid).ToList() ??
new List<Comment>().ToList();
}
Look at this example:
List<string> test = new List<string>();
var test1 = test.Where(x => x == "a").ToList();
If test exists but is empty the query returns an empty list. If test is null the query throws an error. So you can adapt the query as follows
List<string> test = new List<string>();
test = null;
var test1 = test?.Where(x => x == "a") ?? new List<string>().ToList();
The query is now 'safe'. Both of the above examples return an empty list i.e. test1.Count() will return zero but will be usable.
You can also look at the definitions of Where and ToList

Return Linq query results into List object

I am trying to return the results of a query into a List object, however the following code, as I normally use, does not work. Still relatively new to Linq, can someone explain the correct syntax/what's going on? This will work if I change the data type of productTraining to var...
List<AgentProductTraining> productTraining = new List<AgentProductTraining>();
productTraining = from records in db.CourseToProduct
where records.CourseCode == course.CourseCode
select records;
Select() and Where() will return IQueryable<T>, not List<T>. You've got to convert it to a List<T> - which actually executes the query (instead of just preparing it).
You just need to call ToList() at the end of the query. For example:
// There's no need to declare the variable separately...
List<AgentProductTraining> productTraining = (from records in db.CourseToProduct
where records.CourseCode == course.CourseCode
select records).ToList();
Personally I wouldn't use a query expression though, when all you're doing is a single Where clause:
// Changed to var just for convenience - the type is still List<AgentProductTraining>
var productTraining = db.CourseToProduct
.Where(records => records.CourseCode == course.CourseCode)
.ToList();

Getting Last rows from the result of Linq to Sql statement

I couldn't get last articles of every writers in this statement.
List<Editor> lstEditors = dataContext.GetTable<Editor>().Where(t => t.M_Active).Select(t => t).ToList();
var lstArticles = from article in DAO.context.GetTable<Article>().ToList()
join editor in lstEditors on article.RefEditorId equals editor.EditorId
select
new
{
article.M_ArticleId,
article.M_Subject,
article.M_Text,
editor.M_EditorId,
editor.M_Member.M_EditorPicture,
M_NameSurname = editor.M_Member.M_Fname + " " + editor.M_Member.M_Lname
};
Be careful, your query is fetching all the contents of both the Editor and the Yazi tables and then performs Linq-to-Objects on it.
I'm not sure what you ask exactly either, do you want to obtain the list of all writers (editors) along with the last article of each one of these writers?
Do you want to get the writers that did not write any articles yet also?
Edit:
explanation of methods causing an immediate query
Any time you call one of the methods listed below on an IQueryable object (tables or other queries), it performs the actual query to SQL server:
ToList(), ToArray(), ToLookup(), ToDictionay()
Count(), Sum(), Avg(), Aggregate(), Min(), Max()
First(), FirstOrDefault(), Last(), LastOrDefault()
getting last article written by each writer
//create a subquery that returns an editor and its last article date
var editorLastArticleDates =
from article in DAO.context.GetTable<Article>()
group article by article.RefEditor into g
let lastArticleDate= g.Max(x => x.Date)
select new
{
Editor = g.Key,
LastArticleDate = lastArticleDate,
};
//Note: We did not do a ToList() here so the query is not executed
// The editorLastArticleDates object is a IQueryable<>
var query =
from article in DAO.context.GetTable<Article>()
join editorLastArticleDate in editorLastArticleDates
on new { article.Editor, article.Date } // 1
equals new { editorLastArticleDate.Editor, // 2
Date = editorLastArticleDate.LastArticleDate } // 3
select new
{
article.M_ArticleId,
article.M_Subject,
article.M_Text,
article.RefEditor.M_EditorId,
article.RefEditor.M_Member.M_EditorPicture,
M_NameSurname = article.RefEditor.M_Member.M_Fname + " "
+ article.RefEditor.M_Member.M_Lname,
};
//Note: We did not do a ToList() yet so the query is not executed
// The query object is a IQueryable<>
Console.WriteLine(query.ToString()); //Displays SQL query on the console
var results = query.ToList(); // SQL query is executed on this line.
In the code above, I left some remarks on things I had problems with:
When using join, the section between new and equals access only variables declared before the join keyword while the section after the equals keyword has access to the variable defined between join and in.
When writing your join condition, make sure you use equals and not ==.
When using new { XXX, YYY } syntax in your join condition, you declare anonymous types. If the property names are not identical on both sides, it will not compile. In order to have identical property names in this sample, I added the Date = before my value.
By the way, you should use LinqPad to test your queries, it is really a nice tool.

When IQueryable is created from a linq query why is it not a "new" variable?

I am using the Entity Framework and have got a loop that looks at a set of People and using a foreach loop creates a query for the address of each person. As each address query is created it is added to the node of a treeview where it can be later used (to populate children nodes):
IQueryable<Person> pQuery = (IQueryable<Person>)myContext.People; //get a list of people
//go through and get the set of addresses for each person
foreach (var p in pQuery)
{
var addressQuery = from a in myContext.Addresses
from al in a.Address_Links
where al.P_ID == p.P_ID
orderby a.A_POST_CODE
select a;
//add the query to a TreeView node (use the tag to store it)
TreeNode newNode = new TreeNode();
newNode.Tag = addressQuery;
}
Now, the problem that I am finding upon running the app is that ALL the queries are the last query created i.e. the last iteration of the loop. It is like the addressQuery is created on the first iteration of the loop and then overwritten on each subsequent query. The result of this is that it is like all the address queries in the treenodes are references to the last query made(?)
Further investigation that I could solve the problem by using a static class to generate the address query and pass that into each the TreeNode, as follows:
public static class Queries
{
public static IQueryable<Address> AddressesForPerson(GenesisEntities myContext, int key)
{
var query = from a in myContext.Addresses
from al in a.Address_Links
where al.P_ID == key
orderby a.A_POST_CODE
select a;
return query;
}
}
The question I have is that I am baffled by this behaviour. Why does having a static query class help me? Can anyone explain to me what is going on?
Confused.Com!
The reason is that the p variable (the foreach loop variable) is captured and the query is evaluated lazily. As a result, when the query is actually run, it uses the current value of p variable at that time, which is the last value. Read my answer to "What is the exact definition of a closure?" for more info.
To solve the problem, simply try introducing a temporary variable:
// `loopVariable` is scoped inside loop body AND the loop declaration.
foreach (var loopVariable in pQuery)
{
var p = loopVariable; // This variable is scoped **inside** loop body.
var addressQuery = from a in myContext.Addresses
from al in a.Address_Links
where al.P_ID == p.P_ID
orderby a.A_POST_CODE
select a;
//add the query to a TreeView node (use the tag to store it)
myTreeView.Tag = addressQuery
}

DataTable Query

I am new to LINQ. I am trying to find the rows that does not exists in the second data table.
report_list and benchmark both type are : DataTable. Both these datatables are being populated using OleDbCommand,OleDbDataAdapter. I am getting an error "Specified cast is not valid." in foreach ... loop. I would appreciate your help.
var result = from a in report_list.AsEnumerable()
where !(from b in benchmark.AsEnumerable()
select b.Field<int>("bench_id")
)
.Contains(a.Field<int>("BenchmarkID"))
select a;
foreach (var c in result)
{
Console.WriteLine(c.Field<string>("Name"));
}
I don't know if I understood your question. Are you trying to get the items that exists in the first table but not in the second?
var first = new string[] { "b", "c" };
var second = new string[] { "a", "c" };
//find the itens that exist in "first" but not in "second"
var q = from f in first
where !second.Contains(f)
select f;
foreach (var s in q) {
Console.WriteLine(s);
}
//Prints:
//b
I suggest you to make the inner query first, once it does not depend on the outer record.
From a in report_list
Group Join b in benchmark On a.bench_id Equals b.bench_id Into g = Group
Where g.Count = 0
Select a
Note that this is VB syntax.
My suspicion is that one of the fields you are comparing is not an integer in the database. I believe that the invalid cast exception is being thrown by one of the Field<int>() calls since that is one of the three different exceptions that this method can throw. See docs here.
Perhaps use the .Except() extension to get the set difference of the two sets?
(from b in benchmark.AsEnumerable()
select new { id = b.Field<int>("bench_id")}).Except(
from a in report_list.AsEnumerable()
select new {id = a.Field<int>("BenchmarkID")})
Not actually sure of the precise syntax, but that should work by taking the ids in benchmark, and then removing all equivalent ids in report_list, leaving only the ids that don't match. (I hope this is the order you were after...)
Note: This is also assuming that the above issue mentioned by tvanfosson isn't also a problem

Resources