I have two dimensional array ICs.each row contains three fields category,name,description.I want to get all distinct category from this array.I had tried something like this
var cs = ICs.Distinct(t => t.Category);
But its not working.Can any one give me a hand on this?
Get all the values and then make a distinct set:
var cs = ICs.Select(t => t.Category).Distinct();
ICS.Select(t=>t.Category).Distinct()
You were close. ;)
Try something like this:
var cs = ICs.Select(t => t.Category).Distinct();
I assume this is what you want - this will return all the distinct categories from the array.
A slightly different version.
var value = (from n in ICs select n.Category).Distinct();
Hope it helps.
Related
I have a list that contains item like below:
Now I want to group according to language index (Languages[x]) like below:
Group-1:
model.Languages[0].Employer
model.Languages[0].Title
Group-2:
model.Languages[1].Employer
model.Languages[1].Title
Can AnyBody tell me how can I do this?
You can use Regex to extract the key:
var grouped = languages.GroupBy(l => Regex.Match(l, #"Languages\[\d+\]").Value);
If you believe your values will be consistent, you can try the below:
var groupedLanguages = languages.GroupBy(c => c.Substring(0, c.LastIndexOf('.')));
That will group by: "model.Languages[xx]"
Hello I'm new to linq and lambda
I have two lists
fl.LocalOpenFiles ...
List<string> f....
there is a property (string) for example taking index 0
fl.LocalOpenFiles[0].Path
i wanted to select all from the first list fl.LocalOpenFiles where fl.LocalOpenFiles.Path starts with a string from the List<string> f
I finally got this...
List<LocalOpenFile> lof = new List<LocalOpenFile>();
lof = fl.LocalOpenFiles.Join(
folders,
first => first.Path,
second => second,
(first, second) => first)
.ToList();
But its just selecting folders that meet the requirement first.Path == second and i couldnt find a way to get the data that i want which is something meeting this "braindump" requirement:
f[<any>] == fl.LocalOpenFiles[<any>].Path.Substring(0, f[<any>].Length)
Another Example...
List<string> f = new List<string>{ "abc", "def" };
List<LocalOpenFile> lof = new List<LocalOpenFile>{
new LocalOpenFile("abc"),
new LocalOpenFile("abcc"),
new LocalOpenFile("abdd"),
new LocalOpenFile("defxsldf"),)}
// Result should be
// abc
// abcc
// defxsldf
I hope i explained it in a understandable way :)
Thank you for your help
Do you mean something like this :
List<LocalOpenFile> result =
lof.Where(file => f.Any(prefix => file.Path.StartsWith(prefix)))
.ToList();
You can use a regular where instead of a join, which will give you more straight forward control over the selection criteria;
var result =
from file in lof
from prefix in f
where file.Path.StartsWith(prefix)
select file.Path; // ...or just file if you want the LocalOpenFile objects
Note that a file matching multiple prefixes may show up more than once. If that is a problem, you can just add a call to Distinct to eliminate duplicates.
EDIT:
If you - as it seems in this case - only want to know the matching path and not the prefix it matches (ie you only want data from one collection as in this case), I'd go for #har07's Any solution instead.
I am going to ask a very basic question and probably a repeated one but I have a bit different situation.
I want to use "in" operator in Linq.
I have to get all the rows from table which has Id provided
by my array and returns the row if it has. How can I do it.
My array has
var aa="1091","1092","1093" and so on.
and my table uses these Ids as Primary keys
.I have to get all the rows whose Id is contained in the array and I do not want to use S.P.
You can use Enumerable.Contains,
var aa = new string[3] { "1091", "1092", "1093" };
var res = yourDataSource.Where(c => aa.Contains(c.ID));
IN statements are created by using Contains in your Where call. Assuming you use integers as IDs, you could write something like this:
var myArray=new[]{1091,1092,1094};
var myEntities=from entity in myTable
where myArray.Contains(entity.ID)
select entity;
The following lines of code grabs a handful of rows from a database, and puts the value Table.Id into a list of integers. I imagine there is a way to condense this code into a single line, but I'm not sure how.
var result db.Table.Where(a=>a.Value>0).ToList();
List<int> ids = new List<int>();
foreach(var row in result){
ids.Add(row.Id);
}
Any help would be appreciated, thanks!
Edit: My title says array, my example has a list, either one is fine. Sorry if there was any confusion.
var ids = db.Table.Where(a => a.Value > 0).Select(row => row.Id).ToList();
Should do the trick unless my fu is off
Try this:
var result = db.Table.Where(a=>a.Value>0)
.Select(a=>a.Id)
.ToList();
You can also use the List.ForEach Method. So something like:
db.Table.Where(a=>a.Value>0).ToList().ForEach(delegate(Table row) { ids.Add(row.Id); });
Just getting my head around all this LINQ stuff and it seems I'm stuck at the first hurdle.
I have a datatable as such:
OrderNo LetterGroup Filepath
----------- ----------- --------------------------------------------------
0 0 Letters/SampleImage.jpg
0 0 Letters/UKPC7_0.jpg
0 0 Letters/UKPC8_0.jpg
What I need is to get all of the filepaths from the Filepath column into a String array. I thought LINQ would be perfect for this (am I right?), but can't seem to construct the correct query.
Can anyone provide some code samples that would point me in the right direction? I have searched around - but don't seem to be getting anywhere.
There are extension methods which make working with data sets much easier:
using System.Data.Linq;
var filePaths =
from row in dataTable.AsEnumerable()
select row.Field<string>("Filepath");
var filePathsArray = filePaths.ToArray();
You can also use the method syntax to put it in one statement:
var filePaths = dataTable
.AsEnumerable()
.Select(row => row.Field<string>("Filepath"))
.ToArray();
string[] filePaths = (from DataRow row in yourDataTable.Rows
select row["Filepath"].ToString()).ToArray();
If you want to use LINQ all the way, set up your database and create a context object. Then you should be able to do something like this:
var filepaths = from order in _context.Orders
select order.Filepath;
This is assuming your table for the row is named Orders, which I guess by your first column name of order. If you wanted to return a set of the order numbers as well for using later to know where the file path came from you could do something like so:
var results = from order in _context.Orders
select new
{
order.OrderNo,
order.Filepath
}
This would give you a new anonymous type that contained both those values as properties.