Linq - selection item from ListView - c#

How could I select all information about selected item, not only first column but all ?
For the first column I just need :
ListView.Items.AddRange(ListData
.Where(i => string.IsNullOrEmpty(searchBox.Text) || i.ID.StartsWith(searchBox.Text))
.Select(c => new ListViewItem(c.ID))
.ToArray());
Lets say that next columns are : Name, LastName
I know that I have to use Linq segment which looks like :
(...).Select(c => { })
Thanks in advance!

You can use the ListViewItem ctor that accept an array of string (where elements after the first are the subitems)
Assuming your class has the properties LastName and Name
ListView.Items.AddRange(ListData.Where(i =>
string.IsNullOrEmpty(searchBox.Text)
|| i.ID.StartsWith(searchBox.Text))
.Select(c => new ListViewItem // this part
(
new string[]{c.ID, c.Name, c.LastName}
)).ToArray());
If the creation of a single ListViewItem get more complicated, consider using a funcion:
ListView.Items.AddRange(ListData.Where(i =>
string.IsNullOrEmpty(searchBox.Text)
|| i.ID.StartsWith(searchBox.Text))
.Select(c => CreateListViewItemFromElement(c)).ToArray());
private ListViewItem CreateListViewItemFromElement(MyClass element)
{
// handle the element to create a "complete" ListViewItem with subitems
ListViewItem item = new ListViewItem(c.ID);
....
return item;
}
(actually, I would use the latter in every case, it's much more readable to me)

Just init all properties that you need in .Select() method line this:
ListView.Items.AddRange(ListData.Where(i =>
string.IsNullOrEmpty(searchBox.Text)
|| i.ID.StartsWith(searchBox.Text))
.Select(c => new ListViewItem // this part
{
Name = c.ID.ToString(),
Text = c.Name + " " + c.LastName
}).ToArray());
Maby you want to fill different properties, so fill freee to change this part as you want.

Well, the ListViewItem class has 22 (!) constructor overloads, so you can use any of them that supports passing string[] items, for instance this one:
.Select(c => new ListViewItem(new string[] { c.ID, c.Name, c.LastName }))

Related

Cannot access groupBy data Linq C#

Inside method I have a list that contains grouped data:
var listofData = _context.DBONE.where(x => x.Id==3 && x.Status!=0)
.GroupBy(x => new { x.Name, x.Class })
.Select(q => new { Result = q.ToList() }).ToList();
if (methodParam == 10)
{
data = listofData.Where(x => FunctionCheck(---CANNOT ACCESS THE FIELDS FROM GROUP DATA TO PASS AS PARAMETERS---) == 10).ToList();
}
And this is the function that will receive 2 parameter from the grouped data:
private int FunctionCheck(int id, string name)
{...}
But, I cannot access the desired field inside 'listofData'. I can access only in case the listofData is not using groupBy().
If I understand what you are trying to do correctly, you are able to access your data but you are actually creating a "list of a list"
Watch my example, I think I have reproduced your scenario here:
As you can see, I then have a "result" which contains a list of users where Id == 3. The problem is that you create a new anonymous object with a props that is a list. So if you try the last thing you see in my image above, I think you will be able to access your rows.
The reason is that after your GroupBy call, the result is of a grouping type - every item of your list is an Enumerable of the original item, so you would have to operate on that grouping in a following manner:
// Groups such that all items in that group pass your check
listofData
.Where(group => group.All(item => FunctionCheck(item.Id, item.Name) == 10))
.ToList();
// Groups where at least one item matches
listofData
.Where(group => group.Any(item => FunctionCheck(item.Id, item.Name) == 10))
.ToList();
The desired outcome is not really clear from the question but this is the step you are likely missing.
Another approach which might be potentially useful is pre-filter the colleciton of items before grouping them:
var listOfGroupedDatas = _context.DBONE
.Where(x => x.Id ==3 && x.Status != 0 && FunctionCheck(item.Id, item.Name) == 10)
.GroupBy(x => new { x.Name, x.Class })
.ToList();
// This will result in a list of groupings in which all items pass your check
I think you want to call SelectMany to project into one dimensional array.
var listofData = _context.DBONE.where(x => x.Id==3 && x.Status!=0)
.GroupBy(x => new { x.Name, x.Class })
.SelectMany(q => q.ToList()).ToList();

Order by multiple properties

I have list which I want to order by like this
First by "ab"
Then by alphabetical order inside the list by "ab"
Then by "cd"
Then by alphabetical order inside the list by "cd"
Then by "ef"
Then by alphabetical order inside the list by "ef"
and then the rest by alphabetical order
I have this linq query
var groups = profileModel.Groups.
OrderByDescending(i => i.FullName.ToLower().Contains("ab")).
ThenByDescending(i => i.FullName.ToLower().Contains("cd")).
ThenByDescending(i => i.FullName.ToLower().Contains("ef"));
How should I extend this one? Do I have to use group by?
It seems that you want this, then no need to use GroupBy:
var groupOrders = new List<string> { "ab", "cd", "ef" };
var resultList = profileModel.Groups
.Select(x => new { ModelGroup = x, First2Letter = x.FullName.Substring(Math.Min(2, x.FullName.Length)) })
.Select(x => new
{
x.ModelGroup,
x.First2Letter,
Index = groupOrders.FindIndex(s => s.Equals(x.First2Letter, StringComparison.OrdinalIgnoreCase))
})
.OrderByDescending(x => x.Index >= 0) // first all known groups
.ThenBy(x => x.Index)
.ThenBy(x => x.ModelGroup.FullName)
.Select(x => x.ModelGroup)
.ToList();
For custom ordering, you can assign a value to each compare condition and OrderByDescending will order by that. Something like this..
lstModel = profileModel.Groups.OrderByDescending(m => m.FullName.ToLower().Contains("ab") ? 3 :
m.FullName.ToLower().Contains("cd") ? 2 :
m.FullName.ToLower().Contains("ef") ? 1 : 0).ToList();
If I got the problem correctly, this will order items based on what they contain. should work in EF as well.
var orderItems = from item in profileModel.Groups
let name = item.FullName.ToLower()
orderby (name.Contains("ab") ? 1 : 0) + (name.Contains("cd") ? 0.1 : 0) + (name.Contains("ef") ? 0.01 : 0) descending
select item;
EDIT
After rereading the problem this might be the right solution
var orderItems = from item in profileModel.Groups
let name = item.FullName.ToLower()
let order = name.Contains("ab") ? 3 : name.Contains("cd") ? 2 : name.Contains("ef") ? 1 : 0
orderby order descending, item.FullName
select item;
If you might have more or different level1 values to test, you may want a generic version.
Using a convenient extension method FirstOrDefault that takes the default value to return
public static T FirstOrDefault<T>(this IEnumerable<T> src, Func<T, bool> test, T defval) => src.Where(aT => test(aT)).DefaultIfEmpty(defval).First();
You can create an array of first level values in order, and sort first on that, then alphabetically:
var level1 = new[] { "ab", "cd", "ef" };
var ans = groups.OrderBy(i => level1.Select((op, n) => (op, n))
.FirstOrDefault(opn => i.FullName.Contains(opn.op),
(op: String.Empty, n: level1.Length)).n)
.ThenBy(i => i.FullName);

Concatenate two fields in select list (C#)

I use a viewbag to create a select list and I want to Show two fields concatenated together. However, it is crashing on my view. Here is the viewbag code:
ViewBag.PackageId = new SelectList(db.Packages.Where(p => p.status == "A"), "u_package_id", "u_package_id" + "'-'" + "package_nme");
This should work
ViewBag.PackageId = db.Packages.Where(p => p.status == "A")
.Select(p => new SelectListItem
{
Text = p.u_package_id + "-" + p.package_nme,
Value = p.u_package_id
};
The 2nd and 3rd parameters of the SelectList constructor are strings that must match the names of properties in your model (in your case your don't have a property named "u_package_id-package_nme" hence the error).
In the controller, generate a collection of SelectListItem
ViewBag.PackageList = db.Packages.Where(p => p.status == "A").Select(p => new SelectListItem()
{
Value = p.u_package_id, // may need .ToString() depending on the property type
Text = string.Format("{0}-{1}", p.u_package_id, p.package_nme)
}
Side note: Suggest you name your properties to reflect what they are (i.e. its a collection of items, not an ID so PackageList, not PackageId) and this would be necessary anyway if the model your binding to contains a property named PackageId
Thanks to deramko, I have my answer. He was 99% of the way there. Here is the final code:
ViewBag.PackageId = db.Packages.Where(p => p.status == "A")
.Select(p => new SelectListItem
{
Text = p.u_package_id + "-" + p.package_nme,
Value = p.u_package_id.ToString()
});

Identifying and grouping similar items in a collection of strings

I have a collection of strings like the following:
List<string> codes = new List<string>
{
"44.01", "44.02", "44.03", "44.04", "44.05", "44.06", "44.07", "44.08", "46", "47.10"
};
Each string is made up of two components separated by a full stop - a prefix code and a subcode. Some of the strings don't have sub codes.
I want to be able combine the strings whose prefixes are the same and output them as follows with the other codes also:
44(01,02,03,04,05,06,07,08),46,47.10
I'm stuck at the first hurdle of this, which is how to identify and group together the codes whose prefix values are the same, so that I can combine them into a single string as you can see above.
You can do:
var query = codes.Select(c =>
new
{
SplitArray = c.Split('.'), //to avoid multiple split
Value = c
})
.Select(c => new
{
Prefix = c.SplitArray.First(), //you can avoid multiple split if you split first and use it later
PostFix = c.SplitArray.Last(),
Value = c.Value,
})
.GroupBy(r => r.Prefix)
.Select(grp => new
{
Key = grp.Key,
Items = grp.Count() > 1 ? String.Join(",", grp.Select(t => t.PostFix)) : "",
Value = grp.First().Value,
});
This is how it works:
Split each item in the list on the delimiter and populate an anonymous type with Prefix, Postfix and original value
Later group on Prefix
after that select the values and the post fix values using string.Join
For output:
foreach (var item in query)
{
if(String.IsNullOrWhiteSpace(item.Items))
Console.WriteLine(item.Value);
else
Console.WriteLine("{0}({1})", item.Key, item.Items);
}
Output would be:
44(01,02,03,04,05,06,07,08)
46
47.10
Try this:-
var result = codes.Select(x => new { SplitArr = x.Split('.'), OriginalValue = x })
.GroupBy(x => x.SplitArr[0])
.Select(x => new
{
Prefix= x.Key,
subCode = x.Count() > 1 ?
String.Join(",", x.Select(z => z.SplitArray[1])) : "",
OriginalValue = x.First().OriginalValue
});
You can print your desired output like this:-
foreach (var item in result)
{
Console.Write("{0}({1}),",item.Prefix,item.subCode);
}
Working Fiddle.
Outlined idea:
Use Dictionary<string, List<string>> for collecting your result
in a loop over your list, use string.split() .. the first element will be your Dictionary key ... create a new List<string> there if the key doesn't exist yet
if the result of split has a second element, append that to the List
use a second loop to format that Dictionary to your output string
Of course, linq is possible too, e.g.
List<string> codes = new List<string>() {
"44.01", "44.05", "47", "42.02", "44.03" };
var result = string.Join(",",
codes.OrderBy(x => x)
.Select(x => x.Split('.'))
.GroupBy(x => x[0])
.Select((x) =>
{
if (x.Count() == 0) return x.Key;
else if (x.Count() == 1) return string.Join(".", x.First());
else return x.Key + "(" + string.Join(",", x.Select(e => e[1]).ToArray()) + ")";
}).ToArray());
Gotta love linq ... haha ... I think this is a monster.
You can do it all in one clever LINQ:
var grouped = codes.Select(x => x.Split('.'))
.Select(x => new
{
Prefix = int.Parse(x[0]),
Subcode = x.Length > 1 ? int.Parse(x[1]) : (int?)null
})
.GroupBy(k => k.Prefix)
.Select(g => new
{
Prefix = g.Key,
Subcodes = g.Where(s => s.Subcode.HasValue).Select(s => s.Subcode)
})
.Select(x =>
x.Prefix +
(x.Subcodes.Count() == 1 ? string.Format(".{0}", x.Subcodes.First()) :
x.Subcodes.Count() > 1 ? string.Format("({0})", string.Join(",", x.Subcodes))
: string.Empty)
).ToArray();
First it splits by Code and Subcode
Group by you Code, and get all Subcodes as a collection
Select it in the appropriate format
Looking at the problem, I think you should stop just before the last Select and let the data presentation be done in another part/method of your application.
The old fashioned way:
List<string> codes = new List<string>() {"44.01", "44.05", "47", "42.02", "44.03" };
string output=""
for (int i=0;i<list.count;i++)
{
string [] items= (codes[i]+"..").split('.') ;
int pos1=output.IndexOf(","+items[0]+"(") ;
if (pos1<0) output+=","+items[0]+"("+items[1]+")" ; // first occurence of code : add it
else
{ // Code already inserted : find the insert point
int pos2=output.Substring(pos1).IndexOf(')') ;
output=output.Substring(0,pos2)+","+items[1]+output.Substring(pos2) ;
}
}
if (output.Length>0) output=output.Substring(1).replace("()","") ;
This will work, including the correct formats for no subcodes, a single subcode, multiple subcodes. It also doesn't assume the prefix or subcodes are numeric, so it leaves leading zeros as is. Your question didn't show what to do in the case you have a prefix without subcode AND the same prefix with subcode, so it may not work in that edge case (44,44.01). I have it so that it ignores the prefix without subcode in that edge case.
List<string> codes = new List<string>
{
"44.01", "44.02", "44.03", "44.04", "44.05", "44.06", "44.07", "44.08", "46", "47.10"
};
var result=codes.Select(x => (x+".").Split('.'))
.Select(x => new
{
Prefix = x[0],
Subcode = x[1]
})
.GroupBy(k => k.Prefix)
.Select(g => new
{
Prefix = g.Key,
Subcodes = g.Where(s => s.Subcode!="").Select(s => s.Subcode)
})
.Select(x =>
x.Prefix +
(x.Subcodes.Count() == 0 ? string.Empty :
string.Format(x.Subcodes.Count()>1?"({0})":".{0}",
string.Join(",", x.Subcodes)))
).ToArray();
General idea, but i'm sure replacing the Substring calls with Regex would be a lot better as well
List<string> newCodes = new List<string>()
foreach (string sub1 in codes.Select(item => item.Substring(0,2)).Distinct)
{
StringBuilder code = new StringBuilder();
code.Append("sub1(");
foreach (string sub2 in codes.Where(item => item.Substring(0,2) == sub1).Select(item => item.Substring(2))
code.Append(sub2 + ",");
code.Append(")");
newCodes.Add(code.ToString());
}
You could go a couple ways... I could see you making a Dictionary<string,List<string>> so that you could have "44" map to a list of {".01", ".02", ".03", etc.} This would require you processing the codes before adding them to this list (i.e. separating out the two parts of the code and handling the case where there is only one part).
Or you could put them into a a SortedSet and provide your own Comparator which knows that these are codes and how to sort them (at least that'd be more reliable than grouping them alphabetically). Iterating over this SortedSet would still require special logic, though, so perhaps the Dictionary to List option above is still preferable.
In either case you would still need to handle a special case "46" where there is no second element in the code. In the dictionary example, would you insert a String.Empty into the list? Not sure what you'd output if you got a list {"46", "46.1"} -- would you display as "46(null,1)" or... "46(0,1)"... or "46(,1)" or "46(1)"?

Convert IDictionary<Guid,string> to the IEnumerable<SelectListItem>

How i can convert IDictionary<Guid,string> to the IEnumerable<SelectListItem>?
I want to use string as SelectListItem.
If you want to use the guid as the value, you can use
dictionary.Select(x => new SelectListItem { Text = x.Value, Value = x.Key })
Well you could just use
dictionary.Values().Select(x => new SelectedListItem { Text = x })
Just be aware that it may not be in a useful order: Dictionary<,> is inherently unordered (or rather, the order may change and shouldn't be relied on).
Something like this should do what you want:
var selectList = dictionary
.OrderBy(kvp => kvp.Value) // Order the Select List by the dictionary value
.Select(kvp => new SelectListItem
{
Selected = kvp.Key == model.SelectedGuid, // optional but would allow you to maintain the selection when re-displaying the view
Text = kvp.Value,
Value = kvp.Key
})
.ToList();
Using LINQ, you could do something like,
var theSelectList = from dictItem in dict
select new SelectListItem()
{
Text = dictItem.Value,
Value = dictItem.Key.ToString()
};

Categories

Resources