Halalbin Halalbin - 1 year ago 66
C# Question

Get data from with dynamic where argument

A table in a database that contains three columns, Id, Word, Opposite.(In the context below the "Word" and "Opposite" is the actual data/values in the table) The values from Word is get by a LINQ-quey in a controller and sent to the view and presented as list. Next to the Word ther is a

where the user is supposed to type in the Opposite. In the view a DisplayFor(thats Word) and a EditorFor(a input box) gets a zero-based index. The user input is sent to the controller that passes the model as parameter. From the model the guessed Opposites is found and put in a
List<string> and is compared to another
that contains the actual values from the column

As of now I can get the user input from the
and from the data i have i can get the correct guessed
and the wrongly guessed
. But I can´t get the values from the column
from the same row of
that is either correct or wrong.

Opposite is something like

var list = oppositeListFromDB.Except(guessesFromUser);

My initial thought was make another LINQ query with the values that is the correct Opposite and select the matcing Word. To clarify If the user guesses wrong on row 5 and 6, the words "Dry" and "Old" is used as a where parameter in a list. But the parameters must be dynamic and how do i solve this? I've tried something like below but it does not work. I'am thinking correctly or is there a better approach?

var q = from a in db.Table
where a.Opposite.Contains(list)
select a.Word;

Using MVC 5 and have a model like below, the List is used in another part of the projekt. Could I use it for my problem here btw?

public class MyModel
public string Word { get; set; }
public string Opposite { get; set; }
public List<WordCl> mWords { get; set; }

public class WordCL
{ public int Id { get; set; }
public string Word { get; set; }
public string Opposite { get; set; }

| ID | Word | Opposite |
| 1 | Rain | Sun |
| 2 | Clean | Dirty |
| 3 | Light | Dark |
| 4 | Hot | Cold |
| 5 | Wet | Dry |
| 6 | Young | Old |


apk apk
Answer Source

I have a hard time visualizing which data you have available and which data you're trying to collect.

You can use an anonymous type object to select fields that interest you:

var result = from a in db
             where a.Word == "..."
             select new { a.Opposite, a.Word };

foreach (var r in result)

By the way (can't comment):

where a.Opposite.Contains(list)

Shouldn't this be where list.Contains(a.Opposite), as otherwise you'd be calling Contains on a string?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download