ITsolutionWizard
asked on
c# & linq
I have below codes with linq and it works. It return 1 LIst<string> from x.UI_Benefit.
However, I want to return two List<string> From new column call x.UI_Amount.
How can I return two list<string> from one ReadUI method?
Thanks
public static List<string> ReadUI(string selectString)
{
Repository.Rater.Life.Carr iers.Ameri canGeneral .ACPBaseRa teDataCont ext baseRateDataContext = new Repository.Rater.Life.Carr iers.Ameri canGeneral .ACPBaseRa teDataCont ext();
return (from x in baseRateDataContext.QT_AG_ AC_ACP_Bas eRates
where x.UI_Benefit == selectString
select x.UI_Benefit
).ToList();
}
However, I want to return two List<string> From new column call x.UI_Amount.
How can I return two list<string> from one ReadUI method?
Thanks
public static List<string> ReadUI(string selectString)
{
Repository.Rater.Life.Carr
return (from x in baseRateDataContext.QT_AG_
where x.UI_Benefit == selectString
select x.UI_Benefit
).ToList();
}
A function can only have a single return value. You will have to create a class with two properties, each of which is a list. Populate the properties of that class in your method and return that.
you can take help of objects in this case
the other way is you can try the sample below
say for example
the other way is you can try the sample below
say for example
public static List<List<string>> ReadUI(string selectString)
{
List<List<string>> array_list = new List<List<string>>();
array_list.Add(Repository.Rater.Life.Carriers.AmericanGeneral.ACPBaseRateDataContext baseRateDataContext = new Repository.Rater.Life.Carriers.AmericanGeneral.ACPBaseRateDataContext();
return (from x in baseRateDataContext.QT_AG_AC_ACP_BaseRates
where x.UI_Benefit == selectString
select x.UI_Benefit).ToList());
// add the othere list element below
}
hope this helps.
@Jitendra... My big concern there would be maintainance and readability over functionality :) but nice example of the power of LINQ
ASKER
Jitendra Patil: Your code still return one column only (UI_Benefit),
I do not know what you mean by add the others list element below.
please show me in codes if possible.
I do not know what you mean by add the others list element below.
please show me in codes if possible.
I would go with something like this, notice that i introduce a class BenefitsData (you could use an anonymous return type if you like)
public static BenefitsData ReadUI(string selectString)
{
BenefitsData data = new BenefitsData();
var baseRateDataContext = new Repository.Rater.Life.Carriers.AmericanGeneral.ACPBaseRateDataContext();
var query = (
from x in baseRateDataContext.QT_AG_AC_ACP_BaseRates
where x.UI_Benefit == selectString
).ToList();
data.Benefits = query.Select(x => x.UI_Benefit).Distinct().ToList();
data.Amounts = query.Select(x => x.UI_Amount).Distinct().ToList();
}
public class BenefitsData
{
public List<string> Amounts { get; set; }
public List<string> Benefits { get; set; }
}
and to use
//to use
var result = ReadUI("myfilter");
result.Amounts
result.Benefits
IT Solutionwizard sorry for the late respone, as MlandaT described in the above example, this was what i suggested in my first comment.
MlandaT great example above, maintainance and readability wise its good. Thanks for the example.
you can take help of objects in this case
MlandaT great example above, maintainance and readability wise its good. Thanks for the example.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.