Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I try to group by StringId and then by Name
var a = myList
.GroupBy(item => new TcGroupByKey()
{StringId = item.Id, ChannelName = item.ChannelName}).ToList();
I have created this inner class TcGroupByKey
so I can pass the result to TrackingChannelRow ctor
and will get strongly type argument and not object
public TrackingChannelRow(ManageTcModel.TcGroupByKey tcGroupByKey,
IGrouping<ManageTcModel.TcGroupByKey, TrackingChannelItem> subChannels,
IEnumerable<Manager.TrackingChannels.Model.ToolbarItem> toolbars,
IEnumerable<Manager.TrackingChannels.Model.BundleItem> bundles)
{
But the group by doesn't work.
What am I doing wrong?
I think that what you're looking for is to group by composite key...
Something similar to what Marc described in this post Linq to SQL with Group by
The key is to use 'anonymous' types - not the named one like you did - that LINQ can translate into SQL, otherwise there's an issue and that code can only run 'post SQL' (so you enumerate first then group but lose on the SQL integration, performance).
So, in your case it'd be something like this...
new {StringId = item.Id, ChannelName = item.ChannelName}
...instead of new TcGroupByKey() {StringId = item.Id, ChannelName = item.ChannelName}
There is similar problem with doing a 'Select' into anonymous (works) or named type (doesn't - unless, as I mentioned above, you separate the SQL and C#-only expressions part, a bit simplified).
hope this helps
You are asking linq to group your list by an object he doesn't recognize.
What's wrong with:
var a = myList.GroupBy(Id).GroupBy(ChannelName).ToList();
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
How does the below statement work?
I tried to see in Microsoft documentation, but couldn't find much information
Link to Microsoft documentation
var queryString = this.Request.Query[SomeConstant.QueryString];
Lets assume you hit any of your endpoint with
/someendpoint?foo=bar&name=Vamsi
You can now get the whole QueryCollection with:
var queries = this.Request.Query;
If you want to retrieve a specific value by its key, you can use that:
var foo = this.Request.Query["foo"] //value: bar
var name = this.Request.Query["name"] //value: Vamsi
So to answer your question: Query[SomeConstant.QueryString] is accessing the QueryCollection of the current request by accessing a specific key, that is stored in a variable called SomeContant.QueryString
The query object here is an IQueryCollection.
The IQueryCollection implements both :
IEnumerable<KeyValuePair<String,StringValues>>
IEnumerable
Suppose we have the following Url : http://localhost/home/index?code=A000
The key value pair you can see it as a dictionary, we have a key that represent the query string parameter name (ex: code) and we had a value (ex: A000)
In order to retrieve the code from the url, you have to search in that list and find this name. To do that, you call Query["code"]
In your case SomeConstant.QueryString is a constant defined somewhere in your projet in a class with name SomeConstant and a const with name QueryString and the value of this const is "code".
public Class SomeConsant{
public const string SomeConstant = "code";
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I'm trying to do something like -
List<int> accountList = new List<int>();
accountList .Add(1);
accountList .Add(27);
var rec = _db.Accounts.Where(a=> accountList.Contains(a.accountId)).Take(10);
My code is a little more complicated than this - there are several other conditions in the where clause, but this is the bit that is causing problems - nothing gets returned even when there are matching values.
Basically I want it to retrieve all the records where accountId matches a value in my list.
Any pointers?
The sample above is giving me a cant convert lambda error.
You are missing a bit on your contains version
var hold2 = _db.Accounts.Where(a => find.Contains(a.accountDd)).Take(10).ToList();
Have you tried using any
var hold2 = _db.Accounts.Where(a => accountList.Any(m => m == a.accountId)).Take(10).ToList();
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I've always used Contains method to gain SQL IN functionality to my LINQ queries.
But this time, very strange thing occured.
I've got an array of string like this :
string[] FilenamesToParse = {"JOHN_X200-", "DOE_X300-", "FOO_X300_M-"};
Then I've used this array just like below:
var result = (from dps in appProcessList
where FilenamesToParse.Contains(dps.FileName)
select dps.Devices).ToList()
Above query resulted with 0 result but I'm sure that there are filenames contains words defined in FilenamesToParse array.
So I've tried below snippet and Contains worked.
foreach (var applicationProcess in appProcessList)
{
if (applicationProcess.FileName.Contains(FilenamesToParse[0]))
{
}
}
Where am I wrong here ?
Thanks in advance.
Your two appraoches aren´t similar. In your linq you´re iterating your FilenamesToParse-array and check if any of its elements exactly matches dps.FileName, whereby in the second one you iterate appProcessList and check if its FileName-property contains the first FilenamesToParse.
The following would be the linq-approach similar to your loop:
var result = (from dps in appProcessList
where FilenamesToParse.Any(x => dps.FileName.Contains(x))
select dps.Devices).ToList()
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to store my model class as a variable and use it in all my code dynamically. For example I have something like this:
IEnumerable<Student> students = GetAllStudents();
Normally I must always declare and retype Student class. But I need to declare it only once and after that use it dynamically in my code. Simply I want to do something like this:
Student dynamicType;
So after that can be possible:
IEnumerable<dynamicType> students = GetAllStudents();
Can you help me to find right solution? Is it possible to do something like this?
Thank you
It depends what you mean by "dynamic".
If you just don't want to use the class name "Students", then you're looking for a Type Alias.
using dynamicType = MyNameSpace.Student;
...
IEnumerable<dynamicType> students = GetAllStudents();
If you want to be able to specify which type you're dealing with from another part of code, you're looking for Generics.
void DoSomething<T>()
{
IEnumerable<T> things = GetAllThings<T>();
...
}
...
DoSomething<Student>();
If you want to be able to interact with the class without having any specific type associated with it, you're looking for the dynamic keyword.
IEnumerable<dynamic> students = GetAllStudents();
foreach(var student in students)
{
dynamic studentFoo = student.foo; // no type checking
}
If you want to avoid typing "Student" everywhere, but you really do want the items in your IEnumerable to be statically-checked Students, then you may just want to use the var keyword.\
var students = GetAllStudents();
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am trying to do a fuzzy match of records in two Account table using .NET Entity Framework.
I wrote some code like this but it has bad performance like 1 min a record.
ARSalesforceEntities arsf = new ARSalesforceEntities(); //dbcontext
Salesforce_FFEntities ffsf = new Salesforce_FFEntities(); //dbcontext
var araccounts = arsf.Accounts; //dbset contains 400000 records
var ffaccounts = ffsf.Accounts; //dbset contains 6000 records
IDCONV byName = new IDCONV();
IDCONV byAddress = new IDCONV();
foreach (var ffaccount in ffaccounts)
{
Console.WriteLine(++count);
foreach (var araccount in araccounts)//this line goes every slow like 1 min
{
Basically, I am comparing the records in two tables with complicated logic.
How can I greatly improve the performance of the code?
Thank you
This line:
var ffaccounts = ffsf.Accounts;
is what's hitting you hard. You're basically assigning the IQueryable to a variable which, when accessed in your inner loop, re-queries the database everytime. I imagine simply adding ToList() on the end will drastically improve your performance:
var ffaccounts = ffsf.Accounts.ToList();
That's assuming of course that it's acceptable to materialise the 6000 rows into memory.
If not, then you might want to consider writing the logic in SQL and doing all the work in the DB instead...