I'm trying to add a where clause to an existing LINQ DataBind but nothing I do works. The where clause I want to add checks if in the table refAuthSigner the column IsActive == 1.
Here's my existing Query:
// populates Authorized Signer dropdownlist
using (dbPSREntities10 myEntities = new dbPSREntities10())
{
var allSigners = from refAuthSigner in myEntities.refAuthSigners <--- where clause somewhere around here??
select new
{
refAuthSignerID = refAuthSigner.refAuthSignerID,
refAuthSignerName = refAuthSigner.refAuthSignerFirst + " " + refAuthSigner.refAuthSignerLast
};
ddlAuthSigners.DataSource = allSigners;
ddlAuthSigners.DataValueField = "refAuthSignerID";
ddlAuthSigners.DataTextField = "refAuthSignerName";
ddlAuthSigners.DataBind();
}
I want to add a where clause which is something like:
var allSigners = from refAuthSigner in myEntities.refAuthSigners
where refAuthSigner.IsActive == 1
This code isn't right and just wondering how I would incorporate the where clause into the code. Thanks!
Simply use:
where refAuthSigner.IsActive
Since it's a boolean value you cannot compare it to an integer. It is true or false, not 1 or 0. (Some langauges conflate the two, C# is not one of them.)
There is no need to compare IsActive to anything. where needs a boolean value, and IsActive is a boolean value. You already have exactly what you need.
You could make the statement:
var allsigners = refAuthSigner.Where(x => x.refAuthSigner.IsActive)
Try this:
var allSigners = from refAuthSigner in myEntities.refAuthSigners
where refAuthSigner.IsActive
select new
{
refAuthSignerID = refAuthSigner.refAuthSignerID,
refAuthSignerName = refAuthSigner.refAuthSignerFirst + " " + refAuthSigner.refAuthSignerLast
};
Operator of '==' cannot be applied to operands of type 'bool' and 'int'. IsActive is type bit in SqlServer
If this is the error you are getting try using Any instead of Where as it returns bool
// populates Authorized Signer dropdownlist
using (dbPSREntities10 myEntities = new dbPSREntities10())
{
var allSigners = from refAuthSigner in myEntities.refAuthSigners
where refAuthSigner.IsActive
select new
{
refAuthSignerID = refAuthSigner.refAuthSignerID,
refAuthSignerName = refAuthSigner.refAuthSignerFirst + " " + refAuthSigner.refAuthSignerLast
};
ddlAuthSigners.DataSource = allSigners;
ddlAuthSigners.DataValueField = "refAuthSignerID";
ddlAuthSigners.DataTextField = "refAuthSignerName";
ddlAuthSigners.DataBind();
}
Related
Supposing I have the following anonymous type
var g = records.Select(r => new
{
Id = r.CardholderNo,
TimeIn = r.ArriveTime,
TimeOut = r.LeaveTime,
});
Is it possible to do something like the following:
var g = records.Select(r => new
{
Id = r.CardholderNo,
if (condition)
{
TimeIn = r.ArriveTime;
},
TimeOut = r.LeaveTime,
//many more properties that I'd like to be dependant on conditions.
});
How can I achieve an anonymous type based on conditions?
You can do this by using the ternary operator: ?:
The syntax is like this:
TimeIn = condition ? r.ArriveTime : (DateTime?)null // Or DateTime.Min or whatever value you want to use as default
UPDATE
After thinking about your problem for a couple of minutes I came up with the following code that you should never ever use ;)
using System;
class Program
{
static void Main(string[] args)
{
DateTime dt = DateTime.Now;
bool condition = true;
dynamic result = condition ?
(object)new
{
id = 1,
prop = dt
}
:
(object)new
{
id = 2,
};
Console.WriteLine(result.id);
if (condition) Console.WriteLine(result.prop);
}
}
This code should never be used in production because of it's terrible readability and it's really error prone. However, as a learning example of what's possible with the language it's quite nice.
Not directly using an if statement, but you could do it using the ternary operator (assuming TimeIn is of type DateTime):
var g = records.Select(r => new
{
Id = r.CardholderNo,
TimeIn = condition ? r.ArriveTime : (DateTime?)null;
TimeOut = r.LeaveTime
});
Note this will make the property always appear in your Annonymous Type. If this isn't the desired behavior, then you can't do it this way.
I would suggest thinking about the readability of your code and not only about "how can i shorten these few lines so it looks neat".
No. Anonymous types are just like any other type. It has a fixed list of properties. You can't dynamically add or remove properties.
I suggest to either set the property to null, like in the other answers, or use a Dictionary where you add the relevant properties and their values.
If you really need an if (or any another statement) in your Anonymous Type creation, you can try this not-so-pretty solution:
var g = records.Select(r => new
{
Id = r.CardholderNo,
TimeIn = new Func<DateTime?, DateTime?>(x =>
{
if (...)
return x;
else
return null;
}).Invoke(r.ArriveTime),
TimeOut = r.LeaveTime,
});
I have been trying to solve the syntax for a dynamic linq query that is needed in my application.
I have a dynamic query that the where clause needs to be specified to either
GuidPrimaryKey is contained in a list of Guid OR
GuidPrimaryKey is equal to an item in a list of Guid (using some type of for-loop)
I have a Guid[] populated with over 5,000 keys. My Query is set up as
If I do this (as a test) it is successful
data = data.where("GuidPrimaryKey.Equals(#0)",array[0]);
as well as
data = data.where("GuidPrimaryKey.Equals(#0) OR GuidPrimaryKey.Equals(#1)",array[0], array[1]);
I have tried:data = data.where("GuidPrimaryKey.Contains(#0)",array); but that gives an error: No applicable method 'Contains' exists in type 'Guid'.
I also tried setting a loop to go through the elements in the array and set the where clause as a giant string, but that did not work either.
string s = "";
string p = ""
int counter = 0;
foreach(Guid g in Array)
{
s+= "GuidPrimaryKey.Equals(#" counter.ToString() + ") OR";
p += "Array[" counter.ToString() + "],";
counter++;
}
s = s.remove(s.length - 3, 3);
p = p.remove(p.length - 1, 1);
data = data.Where(s,p);
This gives me the error message: No Property or field '1' exists in type 'DynamicClass1'
Any ideas? I need to have the where clause build the query to check to see if the primary key (GuidPrimaryKey) exists in the list of keys (Guid[]).
I'm not sure if this works in the standard Dynamic Linq library, but I just tried this is my open-source version, and it works well:
var data = data.Where("GuidPrimaryKey in #0", array);
This also works:
var data = data.Where("#0.Contains(GuidPrimaryKey)", array);
Here is a full unit test I wrote to confirm this:
[TestMethod]
public void ExpressionTests_ContainsGuid()
{
//Arrange
//Generate some users with Id fields of type Guid
var userList = User.GenerateSampleModels(5, false);
var userQry = userList.AsQueryable();
//Generate a list of values that will fail.
var failValues = new List<Guid>() {
new Guid("{22222222-7651-4045-962A-3D44DEE71398}"),
new Guid("{33333333-8F80-4497-9125-C96DEE23037D}"),
new Guid("{44444444-E32D-4DE1-8F1C-A144C2B0424D}")
};
//Add a valid Guid so that this list will succeed.
var successValues = failValues.Concat(new[] { userList[0].Id }).ToArray();
//Act
var found1 = userQry.Where("Id in #0", successValues);
var found2 = userQry.Where("#0.Contains(Id)", successValues);
var notFound1 = userQry.Where("Id in #0", failValues);
var notFound2 = userQry.Where("#0.Contains(Id)", failValues);
//Assert
Assert.AreEqual(userList[0].Id, found1.Single().Id);
Assert.AreEqual(userList[0].Id, found2.Single().Id);
Assert.IsFalse(notFound1.Any());
Assert.IsFalse(notFound2.Any());
}
I have created a dynamic query which can return a dataset from an external BAQ. I want the dyanmic query to only return the records which meet the parameters I have parsed.
This is the code I have so far:
// DynamnicQuery for BAQ
Epicor.Mfg.Core.Session epiSession = default(Epicor.Mfg.Core.Session);
epiSession = (Epicor.Mfg.Core.Session)POEntryForm.Session;
DynamicQuery dynamicQuery = new Epicor.Mfg.BO.DynamicQuery(epiSession.ConnectionPool);
//Build Data Set
QueryExecutionDataSet executionDS = new QueryExecutionDataSet();
//Build parametors
QueryExecutionDataSet parameters = new QueryExecutionDataSet();
DataRow paramRow = parameters.ExecutionParameter.NewRow();
paramRow["ParameterName"] = "POSuggestionsView.PartNum";
paramRow["ParameterValue"] = "10050886";
paramRow["ValueType"] = "nvarchar(50)";
paramRow["IsEmpty"] = "False";
paramRow["RowIdent"] = "";
paramRow["RowMod"] = "";
paramRow["DBRowIdent"] = new byte[0];
parameters.ExecutionParameter.Rows.Add(paramRow);
// Out variable which indicates if more results are available (likely for use with topNRecords)
bool hasMoreRecords = false;
//Executed named BAQ with parameter...
DataSet results = dynamicQuery.ExecuteByIDParametrized("AD-999-SB_POSuggestion", parameters, "", 0, out hasMoreRecords);
//Message Each Description....
MessageBox.Show("Number of rows in Results = " + results.Tables["Results"].Rows.Count.ToString());
foreach (DataRow item in results.Tables["Results"].Rows)
{
MessageBox.Show("Row Value = " + item["POSuggestionsView.PartNum"].ToString());
}
The code I have created still returns all of the values from the table without restricting the returned rows to the ones which meet the condition of the parameter. Can anyone help me as to why this is happening please?
You need to create a Parameter in the BAQ.
Open the BAQ editor and navigate to the Phrase Build tab, select the table you want to add the parameter to.
Add a new criteria to the table in the section below, the filter type will be "Specified Parameter". Take note of the parameter name.
Save the BAQ.
Back in your customization modify the paramRow["ParameterName"] = the parameter name you created in the BAQ.
My Db column in a string (varchar) and i need to assign it to a int value.
I am using linq to query.Though the code compiles am getting an error at the run time .
Thanks in advance.
PFB my query :
var vlauesCap = from plan in entities.PA_RTM_CAP_Group
select new Business.PartnerProfile.LookUp
{
Id =Convert.ToInt32(plan.cap_group_code),
//(Int32)plan.cap_group_code,
Value = plan.cap_group_name
};
return vlauesCap.ToList();
The EF provider does not know how to translate Convert.ToInt() into SQL it can run against the database. Instead of doing the conversion on the server, you can pull the results back and do the conversion using linq to objects:
// the ToList() here causes the query to be executed on the server and
// the results are returned in a list of anonymous objects
var results = (from plan in entities.PA_RTM_CAP_Group
select new
{
Code = plan.cap_group_code,
Name = plan.cap_group_name
}).ToList();
// the conversion can now be done here using Linq to Objects
var vlauesCap = from r in results
select new Business.PartnerProfile.LookUp
{
Id = Convert.ToInt32(r.Code),
Value = r.Name
};
return vlauesCap.ToList();
You can't do this directly, what you can do is declare a private variable to handle your "mapped" value, and expose the unmapped property...
[Column(Name = "cap_group_code", Storage = "m_cap_group_code")]
private string m_cap_group_code;
public int cap_group_code {
get
{
return Int32.Parse(m_cap_group_code);
}
set
{
m_cap_group_code = value.ToString();
}
}
Try this:
var vlauesCap = from plan in entities.PA_RTM_CAP_Group
select new Business.PartnerProfile.LookUp
{
Id =Convert.ToInt32(plan.cap_group_code),
Convert.ToInt32(plan.cap_group_code),
Value = plan.cap_group_name
};
return vlauesCap.ToList();
Why aren't you using casting for such a purpose, which is a more effective way of achieving this.
Just replace Convert.ToInt32(plan.cap_group_code) with (int)plan.cap_group_code
Do remember, there should be a value in the string and is int, else it will show Exception. If you are not sure about it, then you can further expand the casting to use null coalesciting operator
Assuming sr is an IEnumerable<string>, I want to use code like this to do an inline calculation using two of the items from sr.Lines(). The problem is that the lambda is of type "lambda expression" and not a Decimal, which shares is expecting. Is there any way to do this type of inline method in an object initializer?
var trades =
from line in sr.Lines()
let items = line.Split('|')
select new Trade
{
Total = () => {
return Convert.ToDecimal(items[1]) + Convert.ToDecimal(items[2]);
},
Name = items[3]
}
You want a decimal expression, not a function:
var trades =
from line in sr.Lines()
let items = line.Split('|')
select new Trade
{
Total = Convert.ToDecimal(items[1]) + Convert.ToDecimal(items[2]),
Name = items[3]
};
Just in case someone else ends up here looking for a solution like I did.
Check How to call anonymous function in C#?.
var trades =
from line in sr.Lines()
let items = line.Split('|')
select new Trade
{
Total = ((Func<decimal>)(() =>
{
return Convert.ToDecimal(items[1]) + Convert.ToDecimal(items[2]);
}))(),
Name = items[3]
};