Linq - pulling a value from a null query result - linq-to-sql

I have a linq query that needs to pull a date column out of a row. The expression currently looks like this
myObject.OrderByDescending(s=> s.MyDate).Where(s => s.CRAStatus.Description == "CheckedOut").FirstOrDefault().MyDate)
The problem is that if there are no rows that are "CheckedOut", the query will return a null and attempting to get "MyDate" will throw an exception. We have some verbose solutions, like:
.ForMember(dest => dest.CheckOutDate, opt => opt.MapFrom(src => {
var temp = src.CRAStatusChangeEvents.OrderByDescending(s=> s.MyDate).Where(s => s.CRAStatus.Description == "CheckedOut").FirstOrDefault();
return temp == null ? temp.MyDate : null;
}));
But it would be nice to find something a little more concise. Any Ideas?

Why not
myObject.OrderByDescending(s=> s.MyDate)
.Where(s => s.CRAStatus.Description == "CheckedOut")
.Select(s => s.MyDate as DateTime?)
.FirstOrDefault();
or
myObject.Where(s => s.CRAStatus.Description == "CheckedOut")
.Max(s => s.MyDate as DateTime?);

One option is to set the default if empty to an "empty" instance (think of string.Empty--its a known instance that represents an empty result):
var date = (myObject
.OrderByDescending(s=> s.MyDate)
.Where(s => s.CRAStatus.Description == "CheckedOut")
.DefaultIfEmpty(MyObject.Empty)
.FirstOrDefault()).MyDate;
Here's a snippet that shows how it works:
var strings = new string[]{"one", "two"};
var length =
(strings.Where(s=>s.Length > 5)
.DefaultIfEmpty(string.Empty)
.FirstOrDefault()).Length;
run that and length is 0. Remove the DefaultIfEmpty line and you get a NRE.

var checkedOut = myObject.Where(s => s.CRAStatus.Description == "CheckedOut");
if (checkedOut.Count() > 0) {
var result = checkedOut.Max(s=> s.MyDate).MyDate;
}

How about an extension method?
static class MyObjectEnumerableExtensions
{
public static TMember GetMemberOfFirstOrDefault<TMember>(this IEnumerable<MyObject> items, Func<MyObject, TMember> getMember)
{
MyObject first = items.FirstOrDefault();
if (first != null)
{
return getMember(first);
}
else
{
return default(TMember);
}
}
}
Sample usage:
List<MyObject> objects = new List<MyObject>();
objects.Add(new MyObject { MyDate = DateTime.MinValue });
var filteredObjects = from s in objects where s.MyDate > DateTime.MinValue select s;
DateTime date = filteredObjects.GetMemberOfFirstOrDefault(s => s.MyDate);
Console.WriteLine(date);

Related

System.Collections.Generic.List`1[System.String]" JSON error

I'm trying to use JSON and I was use PostMan to return Response
this error happent
System.Collections.Generic.List`1[System.String]"}
public ActionResult SendVFCode(string Phone_Number)
{
var jsonSerialiser = new JavaScriptSerializer();
string error = "";
var SearchData ="";
if (Phone_Number == null)
{
error = "Must enter your phone number";
}
else if ( (db.PhoneNumbers.Select(x =>x.Id).Count() < 0)
&& (db.Assistant.Select(x =>x.Id).Count()) < 0)
{
error = "There are no data or your account is not activated";
}
else
{
SearchData = db.PhoneNumbers.Include(x => x.Assistant)
.Where(x => x.PhoneNumber == Phone_Number
&& x.Assistant.IsActive == true).Select(xx =>xx.PhoneNumber).ToList().ToString();
}
json = new
{
err = error,
ResultSearchData = SearchData
};
return Content(jsonSerialiser.Serialize(json));
}
SearchData is not a string. Don't declare it as string and don't try to shove a string into it. It's a List (probably of type List<string> or whatever your Phonenumber type is).
var SearchData =""
Should be:
List<string> SearchData;
And your database call should end in .ToList(), not .ToList().ToString().
Note that ToList() followed with ToString() returns fully-qualified name of the list instead of the list contents, hence you should use List<string> to hold result strings (also the list must be instantiated first before used inside if-block). The correct setup should be like this:
public ActionResult SendVFCode(string Phone_Number)
{
var jsonSerialiser = new JavaScriptSerializer();
string error = "";
var SearchData = new List<string>(); // instantiate list of strings
var phoneCount = db.PhoneNumbers.Select(x => x.Id).Count();
var assistantCount = db.Assistant.Select(x => x.Id).Count();
if (Phone_Number == null)
{
error = "Must enter your phone number";
}
else if (phoneCount < 0 && assistantCount < 0)
{
error = "There are no data or your account is not activated";
}
else
{
// assign list from query results
SearchData = db.PhoneNumbers.Include(x => x.Assistant)
.Where(x => x.PhoneNumber == Phone_Number && x.Assistant.IsActive == true)
.Select(xx => xx.PhoneNumber).ToList();
}
var json = new
{
err = error,
ResultSearchData = SearchData
};
return Content(jsonSerialiser.Serialize(json));
}

Conditional Anonymous type

I am working on Web API and using Anonymous type to make JSON as output. I am stuck in the following scenario:
If there is no record(VALUE) available then i don't want to show that KEY. Meaning, Key should only appear when and only when there is value.
Below is the JSON object i am creating -
"TU": [
{
"BLOCK": [
[
"00:00",
"00:59"
]
]
}
],
"WE": [],// empty
"TH": [],// empty
"FR": [],// empty
"SA": [] // empty
Here for Tuesday we do have records and hence its showing but later for WE,TH,FR,SA there are not records and hence i don't want to show them so my result will be MO/TU only.
I am using below code:
var result = new
{
CustomerID = custId,
DeviceID = dId,
Kind = kind,
WebList = filter.Select(filt => new
{
URL = filt.FilterName,
TimeBlockFlag = new ChicoHelper().GetFlag(browserlimit, filt.ID, filt.FilterOptionID, KindId),
DAILY = browserlimit.Where(xx => xx.FilterID == filt.ID && xx.OptionTypeID == daily).Select(xx => xx.BlockTimeLimit).SingleOrDefault(),
WEEKLY = browserlimit.Where(xx => xx.FilterID == filt.ID && xx.OptionTypeID == weekly).Select(xx => xx.BlockTimeLimit).SingleOrDefault(),
MONTHLY = browserlimit.Where(xx => xx.FilterID == filt.ID && xx.OptionTypeID == monthly).Select(xx => xx.BlockTimeLimit).SingleOrDefault(),
HASVALUES = browserlimit.Where(xx => xx.FilterID == filt.ID).Count() > 0 ? 1 : 0,
BLOCKTYPE = new ChicoHelper().GetBlockType(browserlimit,filt.ID,filt.FilterOptionID,KindId),
SU = blockedlimit.Where(x => x.OptionID == sunday && x.FilterID == filt.ID).GroupBy(x => new { x.BlockDay })
.Select(x => new
{
BLOCK = x.Select(y =>
new[] { y.BlockStartTime.MakeFormatedTime(), y.BlockEndTime.MakeFormatedTime() }
)
}),
MO = blockedlimit.Where(x => x.OptionID == monday && x.FilterID == filt.ID).GroupBy(x => new { x.BlockDay })
.Select(x => new
{
BLOCK = x.Select(y =>
new[] { y.BlockStartTime.MakeFormatedTime(), y.BlockEndTime.MakeFormatedTime() }
)
}),
TU = blockedlimit.Where(x => x.OptionID == tuesday && x.FilterID == filt.ID).GroupBy(x => new { x.BlockDay })
.Select(x => new
{
BLOCK = x.Select(y =>
new[] { y.BlockStartTime.MakeFormatedTime(), y.BlockEndTime.MakeFormatedTime() }
)
}),
// if i can put some condition like if there is not record for WE then don't show it.
WE = blockedlimit.Where(x => x.OptionID == wednesday && x.FilterID == filt.ID).GroupBy(x => new { x.BlockDay })
.Select(x => new
{
BLOCK = x.Select(y =>
new[] { y.BlockStartTime.MakeFormatedTime(), y.BlockEndTime.MakeFormatedTime() }
)
}),
The main reason for doing this is to reduce the JSON size which will be consumed by Mobile Devices.
Please help me with this.
The properties of an anonymous type are fixed at compile-time - you can't make them conditional. However, some other approaches you might want to think about:
You could investigate whether a property is still included in the JSON representation if its value is null. If it's not, you could add an extension method NullIfEmpty() which returns null if its input is empty.
You could try performing the JSON conversion from the anonymous type in code first, then delete any properties with an empty set of results, then just return that JSON object from the API. (I don't know Web API myself, but there must be a way of saying "Here's a JSON object - ask it for its string representation" rather than using an anonymous type.)
You could ditch the anonymous type entirely, and build up the JSON representation programmatically, setting just the properties you want.
In any approach, I would strongly advise you to extract a common method to come up with the property value based on a day of the week, so you can have:
...
SU = blockedLimit.GetDayBlocks(sunday),
MO = blockedLimit.GetDayBlocks(monday),
TU = blockedLimit.GetDayBlocks(tuesday),
...
There's no reason to have all that code repeated 7 times. In fact, I'd probably refactor that part before doing anything else - it'll make it easier to experiment.

operator '.' cannot be applied to lambda expression

I'm trying to create linq lambda expression to return customer whose first or last name starts with specific letters. However i get the error on .select saying that:
operator '.' cannot be applied to lambda expression.
public JsonResult GetCust(string term)
{
var data = context.Customers
.Where((dr => dr.First.StartsWith(term) == true) || (dr => dr.Last.StartsWith(term) == true))
.Select(dr => new { Name=String.Concat(dr.First, dr.Last), Adrs = dr.Street, value = dr.CustID })
.Take(10);
return Json(data, JsonRequestBehavior.AllowGet);
}
Any idea how can I return needed data?
In the following line:
.Where((dr => dr.First.StartsWith(term) == true) || (dr => dr.Last.StartsWith(term) == true))
you are using the ||-Operator on two lambda-expressions.
The Where-Clause should more look like this:
.Where(dr => dr.First.StartsWith(term) || dr.Last.StartsWith(term))

Add additional data to Linq to SQL result

I have Linq to SQL query which return one field with Url and I bind it to asp.net combobox. I need to add new virtual record("All Url") to Linq to SQL result before binding the result. How to do that?
var results =
dataAccess.Statistics.
Where(s => s.DateStamp >= minDate && s.DateStamp <= DateTime.UtcNow && s.UserId == userId).
GroupBy(s => s.Url).
Where(g => g.Count() > 1).
OrderBy(g => g.Count()).
Select(g => new { Url = dataAccess.getDomain(g.Key) });
ddlDomain.DataSource = results;
ddlDomain.DataBind();
Add an item to the combobox, then append the databound items:
ddlDomain.Items.Add(new ListItem() { Text = "All Url" });
ddlDomain.AppendDataBoundItems = true;
ddlDomain.DataSource = results;
ddlDomain.DataBind();

performing more than one Where in query return null!!! why? how to fix this?

i have wrote a method that filters output with provided query and return it. when one Where excuted; it return correct output but when more than one Where excuted; output is null and Exception occured with message "Enumeration yielded no results". why? how i can fix it?
public IQueryable<SearchResult> PerformSearch(string query, int skip = 0, int take = 5)
{
if (!string.IsNullOrEmpty(query))
{
var queryList = query.Split('+').ToList();
var results = GENERATERESULTS();
string key;
foreach (string _q in queryList)
{
if (_q.StartsWith("(") && _q.EndsWith(")"))
{
key = _q.Replace("(", "").Replace(")", "");
results = results.Where(q => q.Title.Contains(key, StringComparison.CurrentCultureIgnoreCase));
}
else if (_q.StartsWith("\"") && _q.EndsWith("\""))
{
key = _q.Replace("\"", "").Replace("\"", "");
results = results.Where(q => q.Title.Contains(key, StringComparison.CurrentCulture));
}
else if (_q.StartsWith("-(") && _q.EndsWith(")"))
{
key = _q.Replace("-(", "").Replace(")", "");
results = results.Where(q=> !q.Title.Contains(key, StringComparison.CurrentCultureIgnoreCase));
}
else
{
key = _q;
results = results.Where(q => q.Title.Contains(key, StringComparison.CurrentCulture));
}
}
this._Count = results.Count();
results = results.Skip(skip).Take(take);
this._EndOn = DateTime.Now;
this.ExecutionTime();
return results;
}
else return null;
}
thanks in advance ;)
string key;
foreach (string _q in queryList)
{
Ah, the expression binds to the key variable. You need a new key variable for each execution of the loop.
foreach (string _q in queryList)
{
string key;