LINQ select null values from List - linq

I have a List<string> and it may contain null values in random indexes. I want to check which elements null and select that elements for throwing message.
What i am using;
List<string> getNames = EbaUsers.GetNamesFromIds(activeDirectoryInfo[7],
activeDirectoryInfo[8], activeDirectoryInfo[9], activeDirectoryInfo[10]);
if(getNames[7].Equals(null))
{
MessageBox.Show("getNames[7] is null");
}
if(getNames [8].Equals(null))
{
MessageBox.Show("getNames[8] is null");
}
if(getNames[9].Equals(null))
{
MessageBox.Show("getNames[9] is null");
}
if(getNames[10].Equals(null))
{
MessageBox.Show("getNames[10] is null");
}
I know this is very easy to do with LINQ but i haven't found anywhere.
Thanks for your help.

Sounds like you want to first project the strings to index/value pairs, then pick the elements with null values, and project to just the indexes:
var nullIndexes = names.Select((value, index) => new { value, index })
.Where(pair => pair.value == null)
.Select(pair => pair.index)
.ToList();

You need to get the index by Enumerable.Select() with index and check if the info is null:
var nullInfoIndexes = activeDirectoryInfo.Select((info, index) => new {info, index})
.Where(x => x.info == null)
.Select(x => x.index);
foreach (var index in nullInfoIndexes)
MessageBox.Show("activeDirectoryInfo[" + index + "] is null");

Related

Escape null values LINQ

I have an issue with this linq expression:
var invs = ids.Split(new[] {'|'}, StringSplitOptions.RemoveEmptyEntries)
.Select(x => sitecoreContext.GetItem<Inv>(new ID(x).Guid))
.ToList();
How can I check for null into the .Select? SitecoreContext.GetItem(new ID(x).Guid)) to crash (because items being unpublished, or created but not published) so I need a way to verify first if the item exist, and only then to make the select.
Thank you.
When you call SitecoreContext.GetItem<T>, in the background SitecoreContext gets the item from the database and then it casts it to the T type. And from what I can see, it can throw an exception if there is no item with the specified ID.
What you can do to avoid this exception is split what SitecoreContext does and execute it on your own with a null check in between:
Execute GetItem first
Do the null check
Cast the item to your type:
var invs = ids.Split(new[] {'|'}, StringSplitOptions.RemoveEmptyEntries)
.Select(x => sitecoreContext.Database.GetItem(new ID(x)))
.Where(x => x != null)
.Select(x => sitecoreContext.Cast<Inv>(x))
.ToList();
You can filter all non-null items using a where statement.
var nonNull = list.Where(element => element != null);
I usually use an extension method for this:
public static class EnumerableExtensions
{
public static IEnumerable<T> WhereNotNull<T>(this IEnumerable<T> enumerable)
where T: class
{
return enumerable.Where(element => element != null);
}
}
Given your example, you could use the statement like this:
var invs = ids.Split(new[] {'|'}, StringSplitOptions.RemoveEmptyEntries)
.WhereNotNull()
.Select(x => sitecoreContext.GetItem<Inv>(new ID(x).Guid))
.ToList();
You Can Check Null Using null coalescing operator or ternary Operator in Linq Example is Given Below
`var productTypes = from ProductDto e in Product
select new
{
Id = e.Product != null ? e.Product.ID : 0,
Name = "xyz"
};`

Cannot convert from IEnumerable<List> to List

I am working on these lists to get an item that matches the selected item from the combobox.
private void InitializaMessageElement()
{
if (_selectedTransactionWsName != null)
{
get a transaction webservice name matching the selected item from the drop down here the output=TestWS which is correct
var getTranTypeWsName = TransactionTypeVModel
.GetAllTransactionTypes()
.FirstOrDefault(transTypes =>
transTypes.WsMethodName == _selectedTransactionWsName);
Loop the list of wsnames from the treenode list. Here it gives me all the node I have which is correct.
var wsNameList = MessageElementVModel
.GetAllTreeNodes().Select(ame =>
ame.Children).ToList();//. == getTranTypeWsName.WsMethodName);
find the getTranTypeWsName.WsMethodName in the wsNameList. Here is where I have the problem:
var msgElementList = MessageElementVModel.GetAllTreeNodes()
.Select(ame => ame.Children).Where(c =>
{
c.Where(d => getTranTypeWsName != null && d.Name == getTranTypeWsName.WsMethodName);
return false;
});
my MsgElement list:
var _msgElementList = new ObservableCollection<MessageElementViewModel>(msgElementList);
this.messageElements = _msgElementList;
NotifyPropertyChanged("MessageElements");
}
Here it is returning wrong data. It returns all the list I have a non filtered one.
Why is my out put inconsistent? I am new to LINQ.
This part of your code seems problematic:
var msgElementList = wsNameList.SingleOrDefault(x =>
{
x.Where(ame => ame.Name == getTranTypeWsName.WsMethodName);
return true;
});
You were expecting a list but you used the SingleOrDefault(), which is meant to be used in retrieving a single record.
Try this, which will retrieve msgElementList. No need to get all tree nodes first and store it in wsNameList:
// Assumed that Name is a property of Children
var msgElementList = MessageElementVModel.GetAllTreeNodes()
.Select(ame => ame.Children).Where(c => c.Name == getTranTypeWsName.WsMethodName).ToList();
Here is a solution:
var msgElementList = wsNameList.SelectMany(x => x.Where(ame => getTranTypeWsName != null && ame.Name == getTranTypeWsName.WsMethodName)).ToList();

Set the list value using linq

Hi I want to set the value in the list of objects that matches the given condition in the where clause.Is it possible?
Other work around is to get the list of objects using where clause and then iterate using for Or foreach loop and update the value.
listOfRequestAssigned.Where(x => x.RequestedNumber == CurrentRequest);
I have list listOfRequestAssigned of objects and want to update some propery of the objects that match my search criteria.
class Request
{
bool _requestCompleted;
int _requestedNumber;
public int RequestedNumber
{
get { return _requestedNumber; }
set { _requestedNumber = value; }
}
public bool RequestCompleted
{
get { return _requestCompleted; }
set { _requestCompleted = value; }
}
}
I want to update RequestCompleted property of all objects that match criteria using Linq
You can use ForEach in Linq
listOfRequestAssigned.Where(x => x.RequestedNumber == CurrentRequest).ToList().ForEach(x => x.RequestCompleted = true);
if you have more than one update to do,
listOfRequestAssigned.Where(x => x.RequestedNumber == CurrentRequest).ToList().ForEach(x => { x.RequestCompleted = true; x.OtherProperty = value; } );
Where(...) give you a query, not a Request or a List<Request>. Use FirstOrDefault() if you want to have one (or 0) result, or ToList() if you want to have a list of results on wich you can use ForEach().
In general Linq is a query- not an update tool, but you can use a foreach:
var currentRequests = listOfRequestAssigned
.Where(x => x.RequestedNumber == CurrentRequest);
foreach(var req in currentRequests)
{
req.RequestCompleted = true;
}
Since you have specific collection of type List, you can just use ForEach and a conditional set:
listOfRequestAssigned.Foreach(x => { if (x.RequestedNumber == CurrentRequest) x.RequestCompleted = true;}});
If you had a more generic collection IEnumerable, you can use Select in Linq to build a projection where property will be set as desired (original collection will be left untouched!):
listOfRequestAssigned
.Where(x => x.RequestedNumber == CurrentRequest)
.Select(x => { x.RequestCompleted = true; return x; })
You can use to assign boolean value by following on comparing time. This is the very simplest and smart way for bool property.
listOfRequestAssigned.ForEach(x => x.RequestCompleted = x.RequestedNumber
== CurrentRequest);

LINQ to Entites/IQueryable: Sorting on multiple fields

I have the following that I'd like to sort:
IQueryable<Map> list;
list = from item in ctx.MAP
.Include("C")
.Include("L")
.Include("L.DP")
select item;
return list.OrderBy(m=>(m.L.DP.Name + m.L.Code));
This works, but it sorts alphabetically - so 12 comes before 9. (Assume Code is a numeric field)
What is the best way to sort this so Code is sorted numerically?
You probably want to use the ThenBy extension method to be able to sort by multiple fields ;)
In your case that would be
return list.OrderBy(m=>m.L.DP.Name).ThenBy(m => m.L.Code);
var results = db.Blogs.AsEnumerable()
.Select(sr => new
{
Searchresult = sr,
Words = Regex.Split(sr.Name, #"[^\S\r\n {1,}").Union(Regex.Split(sr.Name2, #"[^\S\r\n]{1,}"))
})
.OrderByDescending(x => x.Words.Count(w => {
foreach (var item in searchTerms)
{
if(w.ToLower().Contains(item))
{
return true;
}
}
return false;
}))
.Select(x => x.Searchresult);

LinQ query - Add Where dynamically

I am having a hard time solving this problem, need code for creating a dynamic linq query in C#, asp.net. I have 5 dropdown list that searches different column in same database table and return item filtered value to a single listbox. The problem is there is no sequence that which or all or any will be selected in DDLs but the combined filtered result should show up in listbox. I have a working query that is searching and returning result in one column at a time for each DDL selection separately. Have to add where clauses with AND to add other DDL selections dynamically to this query. Thanks
public ListItemCollection searchProject(ListItemCollection projList, String searchstr, String columnName)
{
DataSet DSToReturn = new DataSet();
ListItemCollection returnItems = new ListItemCollection();
DataTable results = (from d in ((DataSet)_MyDataset).Tables["Records"].AsEnumerable()
orderby d.Field<string>("Name") ascending
where (d.Field<string>(columnName) != null)
where d[columnName].ToString().ToLower().Contains(searchstr.ToLower())
select d).CopyToDataTable();
foreach (ListItem li in projList)
{
if ((from System.Data.DataRow row in results.Rows
where li.Value.Equals(row["value"].ToString(), StringComparison.InvariantCultureIgnoreCase)
select row["value"]).Count() > 0)
returnItems.Add(li);
}
return returnItems;
}
Here's some example code for how we do it ...
private void DataPortal_Fetch(GoalCriteria criteria)
{
using (var ctx = ContextManager<Data.ExodusDataContext>
.GetManager(Database.ApplicationConnection, false))
{
this.RaiseListChangedEvents = false;
this.IsReadOnly = false;
// set option to eager load child object(s)
var opts = new System.Data.Linq.DataLoadOptions();
opts.LoadWith<Data.Goal>(row => row.Contact);
opts.LoadWith<Data.Goal>(row => row.Sales);
opts.LoadWith<Data.Goal>(row => row.Customer);
ctx.DataContext.LoadOptions = opts;
IQueryable<Data.Goal> query = ctx.DataContext.Goals;
if (criteria.Name != null) // Name
query = query.Where(row => row.Name.Contains(criteria.Name));
if (criteria.SalesId != null) // SalesId
query = query.Where(row => row.SalesId == criteria.SalesId);
if (criteria.Status != null) // Status
query = query.Where(row => row.Status == (int)criteria.Status);
if (criteria.Statuses.Count != 0) // Statuses
query = query.Where(row => criteria.Statuses.Contains((GoalStatus)row.Status));
if (criteria.ContactId != null) // ContactId
query = query.Where(row => row.ContactId == criteria.ContactId);
if (criteria.CustomerId != null) // CustomerId
query = query.Where(row => row.CustomerId == criteria.CustomerId);
if (criteria.ScheduledDate.DateFrom != DateTime.MinValue) // ScheduledDate
query = query.Where(t => t.ScheduledDate >= criteria.ScheduledDate.DateFrom);
if (criteria.ScheduledDate.DateTo != DateTime.MaxValue)
query = query.Where(t => t.ScheduledDate <= criteria.ScheduledDate.DateTo);
if (criteria.CompletedDate.DateFrom != DateTime.MinValue) // ComplatedDate
query = query.Where(t => t.CompletedDate >= criteria.CompletedDate.DateFrom);
if (criteria.CompletedDate.DateTo != DateTime.MaxValue)
query = query.Where(t => t.CompletedDate <= criteria.CompletedDate.DateTo);
if (criteria.MaximumRecords != null) // MaximumRecords
query = query.Take(criteria.MaximumRecords.Value);
var data = query.Select(row => GoalInfo.FetchGoalInfo(row));
this.AddRange(data);
this.IsReadOnly = true;
this.RaiseListChangedEvents = true;
}
}
We just check for a null value assigned to our criteria object, if it's not null then we append it to the query.

Resources