select from list using LINQ's Where [closed]

select from list using LINQ's Where [closed]



I need to get the vehicle type according to vehicle number. I need to select specific column from a list according to another column.



Here is my code:


protected void ddVehicleNo_SelectedIndexChanged(object sender, EventArgs e)

List<Exp_VehicleDTO> odata = (List<Exp_VehicleDTO>)Session["VehicleDTO"];
var vehityeps=odata.Select(x=>x.VehicleNo.Contains(x.VehicleType.Equals(Convert.ToInt32(ddVehicleNo.SelectedValue))))



This code causes error "the best overload method match for "string.contains(string)" has some invalid arguments".



Exp_vehicleDTO class


public class Exp_VehicleDTO

public int CompanyId get; set;

public int VehicleId get; set;

public int VehicleType get; set;

public string VehicleNo get; set;

public int Status get; set;

public DateTime CreatedDateTime get; set;

public string CreatedBy get; set;

public string CreatedMachine get; set;



Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.






What error are you getting, add information to the question

– Div
Sep 12 '18 at 6:45






x.VehicleType.Equals returns a boolean, you're now trying to find a boolean value in a string (VehicleNo). When you see that kind of error message, examine the method you're using and the information you're putting into it carefully.

– John
Sep 12 '18 at 6:49



x.VehicleType.Equals






Equals returns a bool yet Contains expects a string.

– Enigmativity
Sep 12 '18 at 6:51


Equals


bool


Contains


string






Yeah the bigmistake of this question was not giving your data types, and exactly what you wanted to return. though i was amused for 5 mintues.. job done

– Michael Randall
Sep 12 '18 at 6:58







"i did a mistake by converting to int32. " that is not the main mistake. If you want to filter you should use Where and not Select, otherwise you get a collection of bool values. and Contains needs a string as input

– Mong Zhu
Sep 12 '18 at 6:58


Where


Select


bool


Contains


string




2 Answers
2



You can get the vehicle type like so:


int vehicleType = odata.Single(x => x.VehicleNo.Equals(ddVehicleNo.SelectedValue)).VehicleType;



Single will take the first item that matches the condition. Note that it will throw an exception if the item isn't found, or if there are multiple matching items.


Single



If you want to handle the case that the item isn't found, you can do something like this:


var vehicle = odata.SingleOrDefault(x => x.VehicleNo.Equals(ddVehicleNo.SelectedValue));
if (vehicle != null)

var vehicleType = vehicle.VehicleType;

else

// set combobox's SelectedIndex to -1






how can i modify this to avoid exeption if the item isn't found?

– Didu
Sep 12 '18 at 7:22






@Didu updated :-) You can also swap Single/SingleOrDefault for First/FirstOrDefault if you expect multiple items might have the same VehicleNo and you just want the first one.

– John
Sep 12 '18 at 7:23



Single


SingleOrDefault


First


FirstOrDefault


VehicleNo






Works fine. thank you very much :)

– Didu
Sep 12 '18 at 7:25







It was a long road, but someone got there in the end, up vote

– Michael Randall
Sep 12 '18 at 7:31



Difficult to help without knowing what error you are receiving but try changing your code to this:


protected void ddVehicleNo_SelectedIndexChanged(object sender, EventArgs e)

List<Exp_VehicleDTO> odata = (List<Exp_VehicleDTO>)Session["VehicleDTO"];

var vehityeps = odata
.Where(v => v.VehicleNo.ToString() == ddVehicleNo.SelectedValue)
.Select(v => v.VehicleType);



That should populate vehityeps with all VehicleType's where VehicleNo equals what the user has selected in the ddVehicleNo drop down.


vehityeps


VehicleType


VehicleNo


ddVehicleNo



UPDATED



I'm not sure what types are used Exp_VehicleDTO but my guess is VehicleNo is an int. To be safe with my solution this compares the values as strings.


Exp_VehicleDTO


VehicleNo


int






but this gives a type conversion error

– Didu
Sep 12 '18 at 7:04






@Didu remove int.Parse( ) around ddVehicleNo.SelectedValue - Jesse has assumed it's int since you didn't post your class definition on this question.

– John
Sep 12 '18 at 7:05


int.Parse( )


ddVehicleNo.SelectedValue






done. but it recieves cannot implicity convert type string to bool

– Didu
Sep 12 '18 at 7:08






@John thanks for clarifying to @Didu the assumption of an int. The assumption is based on Convert.ToInt32(ddVehicleNo.SelectedValue) from the code within the question.

– Jesse Johnson
Sep 12 '18 at 7:11


int


Convert.ToInt32(ddVehicleNo.SelectedValue)






I would have made the same assumption :-)

– John
Sep 12 '18 at 7:13

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)