LINQ join scoping - linq

Looks like join cannot use sets defined inside query or am I doing something wrong?
from a in new[] {
new { Id = 1 },
new { Id = 2 } }
let bees = new[] {
new { Id = 2 },
new { Id = 3 } }
join b in bees on a.Id equals b.Id
select 1;
This one gives compile time error 'Element "bees" does not exist in the current context.' What's wrong with the query?

This is not legal either way you slice it - you cannot declare a range variable "in the middle" of a join - internally the let clause gets translated to a Select() statement with an anonymous type - but you cannot use Select() either in the middle of the join, you have to move it after the join.

Have a look at this question - I think it covers things:
Can i use join with let in linq - c#
Basically you can only use the let for the query, rather than for joins.
The following does seem to work, but its not as nice:
from a in new[] {
new { Id = 1 },
new { Id = 2 } }
join b in new[] {
new { Id = 2 },
new { Id = 3 } } on a.Id equals b.Id
select 1;

Related

LINQ to Entities does not recognize the method 'System.Windows.Forms.DataGridViewCell get_Item(Int32)' method

Hi im too new on entity framework and im using it on windows form app.
using (GezentiEntities GE = new GezentiEntities())
{
var cities = from c in GE.Cities
where c.CountryId == ((Guid)(dgCountry.SelectedRows[0].Cells[0].Value))
select new { c.Id, Şehir = c.Name };
dgCity.DataSource = cities.ToList();
}
on dgCity.DataSource = cities.ToList(); line it gives me error when im using it with where condition and ((Guid)(dgCountry.SelectedRows[0].Cells[0].Value)) that code works fine it gives me ID.
you may try to get the needed ID out of the linq to entities query
var id = (Guid)dgCountry.SelectedRows[0].Cells[0].Value;
var cities = from c in GE.Cities
where c.CountryId == id
select new { c.Id, Şehir = c.Name };
dgCity.DataSource = cities.ToList();

LINQ anonym object with result to delimited string (LINQ to Entities does not recognize the method 'System.String ToString()' method)

I am trying to get a ; demlimited string of all the relatives that a person has.
Four tables are involved:
USERTAB
PERSON
PERSON_RELATION
RELATION_TAB
Query
from u in USERTAB
select new
{
Person = from p in PERSON where p.USERID == u.USERID
select new
{
PNo = p.NO,
Name = p.NAME
Relatives = (from r in PERSON_RELATION where r.PSEQ == p.PSEQ select new
{
Description = (from rel in RELATION_TYPE where rel.TYPE_SEQ == r.TYPE_SEQ select rel.DESCRIPTION).ToArray() //(or also tried .ToString())
})
}
}
I'd like the Description field to be a ";" delimited list of all the relatives a user (person) has.
Using a ToString on my Relatives object it only fails runtime with. LINQ to Entities does not recognize the method 'System.String ToString()' method
Example: Description = "Father, Brother, Cousin"
You need to be clear about what parts of your query get translated to SQL to run on the server, and what parts run in your local application. The key is to construct a simple query to retrieve all the data, then use .AsEnumerable() to ensure that the remaining transformations don't get translated to SQL, and finally transform the data into a form useful for you. Something like
var query =
from u in USERTAB
select new
{
Person =
from p in PERSON
where p.USERID == u.USERID
select new
{
PNo = p.NO,
Name = p.NAME
Relatives =
from r in PERSON_RELATION
where r.PSEQ == p.PSEQ
select new
{
Description =
from rel in RELATION_TYPE
where rel.TYPE_SEQ == r.TYPE_SEQ
select pos.DESCRIPTION
}
}
}
};
var enumerable =
from u in query.AsEnumerable()
select new
{
Person =
from p in u.Person
select new
{
PNo = p.PNo,
Name = p.Name
Relatives =
string.Join(", ",
from r in p.Relatives
from d in r.Description
select d.Description)
}
}
};
should do the trick.
This should work: write an extension method for strings of array, like so:
public static ToCsv(this string[] strings)
{
return String.Join("," strings);
}
Then just tack .ToCsv() at the end of your .ToArray() call above, and it should do the trick!

Seperate linq results by first letter asp.net mvc 3

I have a linq query which returns results ordered by first letter. Is there a way to return the first letter before the group ordered by that letter? For instance;
**A**
Acountants
Apothecary
**B**
Basketball
Biscuits
and so on. I tried grouping my results like this;
var companyquery = (from c in db.Categories
group c by c.Name.Substring(0, 1)
into cgroup
select new
{
FirstLetter = cgroup.Key,
Names = cgroup
}).OrderBy(letter => letter.FirstLetter);
return View(companyquery);
but got error:
"The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery1[<>f__AnonymousType31[System.String]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[NewAtAClick.Models.Category]'.'
So now I'm using orderby instead of group _ into _ . Here is my query;
var companyquery = (from c in db.Categories
orderby c.Name.Substring(0, 1)
select c);
This returns them in alphebetical order. I tried saying
letter1 = companyquery.ToString().SubString(0,1)
then
return view(letter1 + companyquery.ToList());
But nothing.
Thanks in advance!
The error says it all. Your view expects a model of type IEnumerable<Category> but you passed in something else (in this case because you are creating a new anonymous type via the call to select new { ... }).
Instead you could declare a new type:
public class AlphabeticalMapping<T> {
public char FirstLetter { get; set; }
public List<T> Items { get; set; }
}
And change your query to return:
var companyquery = (from c in db.Categories
group c by c.Name.Substring(0, 1)
into cgroup
select new AlphabeticalMapping<Category>() {
FirstLetter = cgroup.Key,
Items= cgroup.ToList()
}).OrderBy(mapping => mapping.FirstLetter);
And change your view to expect IEnumerable<AlphabeticalMapping<Category>>
What happens if you try this?
var companyquery = (from c in db.Categories
group c by c.Name.Substring(0, 1)
into cgroup
select new,
{
FirstLetter = cgroup.Key,
Names = cgroup
})
.OrderBy(letter => letter.FirstLetter)
.ToDictionary(k => k.FirstLetter, e => e.Names);

Good practice for using "NOT IN" in a LINQ query

I have converted the following SQL query to LINQ with the exception of the "NOT IN" subquery.
What would be the most effective way to implement this using LINQ? Should I use a join instead?
If anybody is able to provide an example or some guidance, I'd appreciate it.
New LINQ Query:
return from objOpenCalls in db.OpenItemss
from objTasks in db.Tasks
.Where(t => (t.Task_ID == objOpenCalls.Parent_Task_ID))
where ((objTasks.Item_ID > 0) && (objTasks.Type_ID > 0) && (objTasks.OwnerTypeItem_ID == user) && (objOpenCalls.CallEnd < DateTime.Now))
orderby objOpenCalls.CallStart descending
select new CallMiniViewModel
{
ID = objOpenCalls.ID,
CallStart = objOpenCalls.CallStart,
Name = objTasks.Task_Title
};
Old SQL Query:
SELECT TOP (100) ta.ID, t.Task_Title, ta.CallStart
FROM OpenItems AS ta INNER JOIN
Tasks AS t ON ta.Parent_Task_ID = t.Task_ID
WHERE
(t.Item_ID > 0) AND (t.[Type_ID] > 0) AND (ta.CallStart > DATEADD(m, -6, GETDATE()))
AND (ta.ID NOT IN (SELECT CallId FROM CallFeedback)) AND (t.OwnerTypeItem_ID = #Username) AND (ta.CallEnd < GETDATE())
ORDER BY ta.CallStart DESC
There are a couple of ways of doing the not in. Below is just a quick sample put in LinqPad as a test.
class MyClass {
public int Id {get;set;}
}
void Main()
{
int[] myItems = new[] { 1, 2, 3, 4, 5, 6 };
IEnumerable<MyClass> classes = new []{
new MyClass { Id = 3 },
new MyClass { Id = 6 },
new MyClass { Id = 8 }
};
var results = from cl in classes
where !myItems.Contains( cl.Id )
select cl;
foreach(var result in results) {
Console.WriteLine( "Class {0}", result.Id);
}
var results2 = from cl in classes
where (
from i in myItems
where i == cl.Id
select i ).Count( ) == 0
select cl;
foreach(var result in results2) {
Console.WriteLine( "Class {0}", result.Id);
}
}
I normally play with code first in LinqPad as it helps me understand any problems, and it will (if you're working with SQL) then show you what SQL the query will generate and you can fine tune a little. Sometimes it takes a little time to get your stuff able to be run in there, but it's worth it with the more complex queries.

In statement for LINQ to objects

Is there an equivalent of a SQL IN statement in LINQ to objects?
Yes - Contains.
var desiredNames = new[] { "Jon", "Marc" };
var people = new[]
{
new { FirstName="Jon", Surname="Skeet" },
new { FirstName="Marc", Surname="Gravell" },
new { FirstName="Jeff", Surname="Atwood" }
};
var matches = people.Where(person => desiredNames.Contains(person.FirstName));
foreach (var person in matches)
{
Console.WriteLine(person);
}
(In LINQ to SQL this ends up as an "IN" query.)
Note that in LINQ to Objects the above isn't really very efficient. You'd be better off with a join:
var matches = from person in people
join name in desiredNames on person.FirstName equals name
select person;
(This could still be done with dot notation of course, but it ends up being somewhat messier.)
I will go for Inner Join in this context. If I would have used contains, it would iterate 6 times despite if the fact that there are just two matches. I just want to emphasize here that I will go for Joins instead of IN predicate.
var desiredNames = new[] { "Pankaj" };
var people = new[]
{
new { FirstName="Pankaj", Surname="Garg" },
new { FirstName="Marc", Surname="Gravell" },
new { FirstName="Jeff", Surname="Atwood" }
};
var records = (from p in people join filtered in desiredNames on p.FirstName equals filtered select p.FirstName).ToList();

Resources